One always wonder about how to insert data into any database from Visual Studio 2008/2010/2012 webpage. Here in this tutorial, we explain the easiest way to do this connection by using Insert Code C#. We will create ASP.NET website in Visual Studio 2010 and use the inbuilt SQL server which comes with it.
Step 1: Create a new Website Project
- Open Visual Studio 2010 and go to File -> New -> Website
- Under the programming language C#, select new ASP.NET Empty Website.
- Rename it as WebsiteTutorial and Click OK.
- Now go to Solution Explorer and you will see the created website.
- Right click on the Website name and add App_Data folder. (Add ASP.NET Folder -> App_Data)
Step 2: Creating the first ASP.NET Webpage
- Right click on Website name and add the first webpage of your website. (Add New Item -> Web Form)
- Make sure to keep C# as programming language.
- Name it as Student.aspx and click Ok.
Step 3: Design a Webpage
- Open the page Student.aspx from Solution Explorer and go to Design view of it. (You will find it right below the webpage)
- Here, we will use HTML Table to keep the design layout proper.
- So, go to Toolbox and drag a Table from HTML section on your web form.
- We will design a Student Details form on this page as shown in the following diagram.
- Make use of Labels and Textbox from Toolbox and change their ‘Text‘, ‘Font‘ and other properties by going to their Properties(Right click on any control -> Properties)
- Finally drag a Button from Toolbox.
Step 4: Design a Database to store the Text
- Here we will add SQL Server Database from Visual Studio itself.
- Right click on Website name -> Add New Item -> SQL Server Database.
- Your database file Database.mdf will get added under App_data folder.
- Next step is to create a table under this database.
- Go to Server Explorer, There you will see Database.mdf file.
- Under that, look for Tables, and Right click -> Add New Table
- Create the table with fields exactly matching to your web form.
- You can set the primary key as Roll_Number by Right clicking on Roll_Number row.
- Save the table as Student.
Step 5: Connecting Web Page to Database Table-Insert Code C#
- We will use the C# code to insert the text from Texbox to Database Table.
- First thing required for this connection is Connection String.
- You can get this connection string in web.config file. (In Solution Explorer)
- To bring this connection string in web.config file, follow this simple trick.
- Look for the table Student you just created in Server Explorer and drag that onto your web form.
- Debug the website. (Debug -> Start Debugging)
- Automatically a Connection String will be generated in web.config file.
- Now go to Web Form and double click on Add Button. You will see the code page Student.aspx.cs.
- Add these two names spaces at the top.
- Paste this Insert Code C# under Button_Click event.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“DatabaseConnectionString1”].ToString());
SqlCommand cmd = new SqlCommand(“Insert into Student(Roll_Number,Fname,Address) values (‘” + TextBox1.Text + “‘,'” + TextBox2.Text + “‘,'” + TextBox3.Text + “‘)”, con);
TextBox1.Text = “”;
TextBox2.Text = “”;
TextBox3.Text = “”;
- DatabaseConnectionString1 is the connection string obtained from web.config file.
- Make sure to give exact table name and it’s column names.
- Also TextBox1, TexBox2 and TextBox3 are the id’s of those corresponding textbox’s on the web form.
Step 6: Run the form
- Save the project and go to Debug -> Start Debugging.
- Your web page will open in a browser.
- Fill the details and click on the Button, Add.
- Check the database table for data. (Right click on Table name i.e. Student -> Show table Data)
You will find the Student Details you just entered in the Web Page form.
You can display a success message on the webpage by putting one Label on the webpage anywhere and keeping it’s Text property blank initially. Later in code, you add this line:
Label4.Text = “Details Added”;
Try this Insert Code C# in Visual Studio 2008, 2010 or 2012 and give us your feedback. Your questions and queries are most welcome and we will try to answer them quickly.
That’s all for the insert operation. Keep tuned on this blog to learn C# coding with Visual Studio.
More tutorial on Visual Studio 2010: How to add Splash Screen to Windows App in VS10 C#
- How To Create Setup file Visual Studio C#.NET Project - September 20, 2016
- How to Create a Simple ASP.NET Website-Master Pages - September 16, 2016
- Retrieve Code C# in Visual Studio 2010 - September 15, 2016
- ASP.NET Validation for a Website-Tutorial - August 31, 2016
- Insert Code C# from Visual Studio into SQL - August 26, 2016
- How to add Splash Screen to Windows App in VS10 C# - August 20, 2016