In this article I will explain with an example, how to
insert data into database using
Stored Procedure in
ASP.Net with C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follow.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
This
Stored Procedure accepts
Name and
Country parameters, which are used to Insert the records in
Customers Table.
CREATE PROCEDURE [Customers_InsertCustomer]
@Name VARCHAR(100),
@Country VARCHAR(50)
AS
BEGIN
INSERT INTO [Customers]
([Name]
,[Country])
VALUES
(@Name
,@Country)
SELECT SCOPE_IDENTITY()
END
HTML Markup
The
HTML markup consists of following controls:
TextBox – For capturing user input.
DropDownList – For selecting Country.
Button – For submitting the Form.
The Button has been assigned with an OnClick event handler.
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Country:</td>
<td>
<asp:DropDownList ID="ddlCountries" runat="server">
<asp:ListItem Text="Please select" Value=" "></asp:ListItem>
<asp:ListItem Text="United States" Value="United States"></asp:ListItem>
<asp:ListItem Text="India" Value="India"></asp:ListItem>
<asp:ListItem Text="France" Value="France"></asp:ListItem>
<asp:ListItem Text="Russia" Value="Russia"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="OnInsert" /></td>
</tr>
</table>
Namespaces
You will need to import the following namespace.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Inserting record in Database using Stored Procedure in ASP.Net
When
Save changes Button is clicked, first the connection is read from
Web.Config file.
Then, an object of SqlCommand class is created and INSERT query is passed to it as parameter.
The values of the Name and Country are added as parameter to SqlCommand object and the ExecuteScalar function is executed.
C#
protected void OnInsert(object sender, EventArgs e)
{
int customerId;
string spName = "Customers_InsertCustomer";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(spName))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text);
cmd.Connection = con;
con.Open();
customerId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Inserted Customer ID: " + customerId + "');", true);
}
VB.Net
Protected Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs)
Dim customerId As Integer
Dim spName As String = "Customers_InsertCustomer"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(spName)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text)
cmd.Connection = con
con.Open()
customerId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Inserted Customer ID: " & customerId & "');", True)
End Sub
Screenshot
The Form
Record after Insert in database
Downloads