Inserting Form Data Into DataBase Using Stored Procedure In ASP.NET C#


I have decided to write this article by considering the requirements of students and beginners that often ask how to insert the form data using a Stored Procedure. So due to this I have explained this requirement with basics so they can get an idea of how to submit form data using a Stored Procedure.

So let us start the walkthrough.

Create a table "Student" as in the following:


Create a Stored Procedure named "Studenentry" as:

Create procedure Studententry
@fname Varchar (50),
@Mname varchar (50),
@Lname Varchar (50)

Insert into Student (Fname,MName,Lastname) values (@fname,@Mname,@Lname)

In the above Stored Procedure I have declared three variables, they are @fname, @Mname and @Lname to store Fname, MName and Lastname into the table.

Now create the one sample application "StudentEntry" as:
  1. Start - All Programs - Microsoft Visual Studio 2010.
  2. File - New Website - C# - Empty website (to avoid adding a master page).
  3. Give the web site a name such as  "StudentEntry" or another as you wish  and specify the location.
  4. Then right-click on Solution Explorer - Add New Item - Default.aspx page.
  5. Drag and drop one button, three textboxes and one label on the <form> section of the Default aspx page.
Then switch to the design view; the <form> section of the Default aspx page source will look as in the following:

<form id="form1"runat="server">
First Name  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Middle Name<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
Last Name <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:ButtonID="Button1"runat="server"Text="Add"onclick="Button1_Click" />
Now double-click on "Button" then write the following code:

protected void Btnsave_Click(object sender, EventArgs e)
      query = "Studententry";         //Stored Procedure name 
      SqlCommand com = new SqlCommand(query, con);  //creating  SqlCommand  object
      com.CommandType = CommandType.StoredProcedure;  //here we declaring command type as stored Procedure

       /* adding paramerters to 
 SqlCommand below *\
       com.Parameters.AddWithValue("@FName",TextBox1.Text.ToString());        //first Name
       com.Parameters.AddWithValue("@Mname ", TextBox2.Text.ToString());     //middle Name
       com.Parameters.AddWithValue("@LName ",TextBox3.Text.ToString());       //Last Name
       com.ExecuteNonQuery();                     //executing the sqlcommand
       Label1.Visible = true;
       Label1.Text = "Records are Submitted Successfully";

In the above code, I have explained detail in each line through comments what each line is used for what purpose, I hope you understand it.

Now run the application; the starting page should be as follows:


Now insert some data and click on the submit button, the following message will be shown after successful submission of the data:


  • For detailed code please download the zip file attached above.
  • Don't forget to Perform changes into the Web.config file according to your Server location. 

Now we have learned how to submit the form data using a Stored Procedure into the database. I hope this article is useful for all students and beginners. If you have any suggestion related to this article please contact me.