Insert Code C# from Visual Studio into SQL

Insert Code C#-technirmiti

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.Insert Code C#-technirmiti
  • 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.Insert Code C#-technirmiti

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.Insert Code C#-technirmiti

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.

Insert Code C#-technirmiti

  • 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.
  1. Look for the table Student you just created in Server Explorer and drag that onto your web form.
  2. Debug the website. (Debug -> Start Debugging)
  3. 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.

using System.Data.SqlClient;
using System.Configuration;

  • Paste this Insert Code C# under Button_Click event.

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“DatabaseConnectionString1”].ToString());
con.Open();
SqlCommand cmd = new SqlCommand(“Insert into Student(Roll_Number,Fname,Address) values (‘” + TextBox1.Text + “‘,'” + TextBox2.Text + “‘,'” + TextBox3.Text + “‘)”, con);
cmd.ExecuteNonQuery();
TextBox1.Text = “”;
TextBox2.Text = “”;
TextBox3.Text = “”;
con.Close();

  • 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#

mm

About Sunayana Gawde

Sunayana Gawde is Chief Operations Officer at TechNirmiti Solutions and the Author at TechOpinion. She is a researcher in the field of Computer Science and Technology. She loves to write about Technology. You can connect with her on LinkedIn and follow her on Twitter.

3 thoughts on “Insert Code C# from Visual Studio into SQL”

Leave a Reply

Your email address will not be published. Required fields are marked *