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:
  1. Create procedure Studententry  
  2. (  
  3. @fname Varchar (50),  
  4. @Mname varchar (50),  
  5. @Lname Varchar (50)  
  7. )  
  8. as  
  9. begin  
  10. Insert into Student (Fname,MName,Lastname) values (@fname,@Mname,@Lname)  
  11. End  
In 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:
  1. <form id="form1"runat="server">  
  2.     <div>  
  3. First Name  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  4. Middle Name<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  5. Last Name <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  6.         <asp:ButtonIDasp:ButtonID="Button1"runat="server"Text="Add"onclick="Button1_Click" />  
  7.     </div>  
  8. </form>  
Now double-click on "Button" then write the following code:
  1. protected void Btnsave_Click(object sender, EventArgs e)  
  2. {  
  3.       connection();  
  4.       query = "Studententry";         //Stored Procedure name   
  5.       SqlCommand com = new SqlCommand(query, con);  //creating  SqlCommand  object  
  6.       com.CommandType = CommandType.StoredProcedure;  //here we declaring command type as stored Procedure  
  8.        /* adding paramerters to  SqlCommand below *\  
  9.        com.Parameters.AddWithValue("@FName",TextBox1.Text.ToString());        //first Name  
  10.        com.Parameters.AddWithValue("@Mname ", TextBox2.Text.ToString());     //middle Name  
  11.        com.Parameters.AddWithValue("@LName ",TextBox3.Text.ToString());       //Last Name  
  12.        com.ExecuteNonQuery();                     //executing the sqlcommand  
  13.        Label1.Visible = true;  
  14.        Label1.Text = "Records are Submitted Successfully";  
  15. }  
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.