Background
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)
-
- )
- as
- begin
- Insert into Student (Fname,MName,Lastname) values (@fname,@Mname,@Lname)
- 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:
- Start - All Programs - Microsoft Visual Studio 2010.
- File - New Website - C# - Empty website (to avoid adding a master page).
- Give the web site a name such as "StudentEntry" or another as you wish and specify the location.
- Then right-click on Solution Explorer - Add New Item - Default.aspx page.
- 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">
- <div>
- 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:ButtonIDasp:ButtonID="Button1"runat="server"Text="Add"onclick="Button1_Click" />
- </div>
- </form>
Now double-click on "Button" then write the following code:
- protected void Btnsave_Click(object sender, EventArgs e)
- {
- connection();
- query = "Studententry";
- SqlCommand com = new SqlCommand(query, con);
- com.CommandType = CommandType.StoredProcedure;
-
- /* adding paramerters to SqlCommand below *\
- com.Parameters.AddWithValue("@FName",TextBox1.Text.ToString());
- com.Parameters.AddWithValue("@Mname ", TextBox2.Text.ToString());
- com.Parameters.AddWithValue("@LName ",TextBox3.Text.ToString());
- com.ExecuteNonQuery();
- 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:
Note
- 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.
Summary
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.