In this article I will explain with an example, how to Insert data into Database using Stored Procedure in ASP.Net using C# and VB.Net.
ADO.Net will be used to perform Insert operation in ASP.Net.


I have made use of the following table Customers with the schema as follows.
Insert data into Database using Stored Procedure in ASP.Net using C# and VB.Net
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file

Stored Procedure

The following Stored Procedure will be used to Insert data into the SQL Server database table.
This Stored Procedure accepts Name and Country parameters, which are used to Insert the records in Customers Table.
CREATE PROCEDURE [dbo].[Customers_InsertCustomer]
      @Name VARCHAR(100),
      @Country VARCHAR(50)
      INSERT INTO [Customers]

HTML Markup

The HTML Markup consists of:
HTML Button – For opening the Bootstrap Modal Popup.
DIV – For Bootstrap Modal Popup contents.
TextBox – For inputting Name.
DropDownList – For selecting Country.
Button – For submitting the Form.
The Button has been assigned with OnClick event handler.
Inside the HTML Form the following Bootstrap CSS is inherited.
1. bootstrap.min.css
Then following jQuery and Bootstrap JS Scripts are inherited.
1. jquery-3.2.1.slim.min.js
2. bootstrap.min.js
<script src=""
    integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<link rel="stylesheet" href=""
    integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"/>
<script src=""
    integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Insert</button>
<div id="exampleModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog"role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Customer Details Form</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
            <div class="modal-body">
                <div class="form-group">
                    <asp:TextBox runat="server" ID="txtName" CssClass="form-control"/>
                <div class="form-group">
                    <asp:DropDownList runat="server" ID="ddlCountries" CssClass="form-control">
                        <asp:ListItem Text="Please select" Value=""/>
                        <asp:ListItem Text="India" Value="India"/>
                        <asp:ListItem Text="China" Value="China"/>
                        <asp:ListItem Text="Australia" Value="Australia"/>
                        <asp:ListItem Text="France" Value="France"/>
                        <asp:ListItem Text="Unites States" Value="Unites States"/>
                        <asp:ListItem Text="Russia" Value="Russia"/>
                        <asp:ListItem Text="Canada" Value="Canada"/>
                <div class="modal-footer">
                    <asp:Button Text="Save changes" runat="server" CssClass="btn btn-primary" OnClick="OnInsert"/>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>


You will need to import the following namespaces.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Inserting record in Database using Stored Procedure in ASP.Net

When the Save changes Button is clicked, following event handler is executed.
Initially, the connection string is fetched from the Web.Config file and object of SqlConnection class is created using it.
Note: For more details on how to read Connection String from Web.Config file, please refer my article Read (Get) Connection String from Web.Config file in ASP.Net using C# and VB.Net.
Then, an object of SqlCommand class is created and the INSERT query is passed to it as parameter.
The values of the Name and Country are added as parameter to SqlCommand object.
And the connection is opened and the ExecuteScalar function is executed.
Note: For more details on how to use ExecuteScalar function, please refer Understanding SqlCommand ExecuteScalar in C# and VB.Net.
Finally, the connection is closed and the CustomerId of the inserted record is fetched and displayed in JavaScript Alert Message Box using RegisterStartupScript method.
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;
            customerId = Convert.ToInt32(cmd.ExecuteScalar());
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Inserted Customer ID: " + customerId + "');", true);
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
            customerId = Convert.ToInt32(cmd.ExecuteScalar())
        End Using
    End Using
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Inserted Customer ID: " & customerId & "');", True)
End Sub


The Form
Insert data into Database using Stored Procedure in ASP.Net using C# and VB.Net
Record after Insert in database
Insert data into Database using Stored Procedure in ASP.Net using C# and VB.Net