In this article I will explain with an example, how to call SQL Server update Stored Procedure using ADO.Net.


I have made use of the following table Customers with the schema as follows.
Calling Update SQL Server Stored Procedures using ADO.Net
I have already inserted few records in the table.
Calling Update SQL Server Stored Procedures using ADO.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 Update data into the SQL Server database table.
This Stored Procedure accepts CustomerId, Name and Country parameters, which are used to UPDATE the records in Customers Table.
CREATE PROCEDURE [Customers_UpdateCustomer]
      @CustomerId INT
     ,@Name VARCHAR(100)
     ,@Country VARCHAR(50)
      UPDATE Customers
      SET [Name] = @Name,
          [Country] = @Country
      WHERE [CustomerId] = @CustomerId

HTML Markup

The HTML Markup consists of:
TextBox – For entering Id, Name and Country.
Button – Submitting the Form.
The Button has been assigned OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
        <td style="width: 60px">Id<br />
            <asp:TextBox ID="txtId" runat="server" Width="50px" />
        <td style="width: 150px">Name<br />
            <asp:TextBox ID="txtName" runat="server" Width="140px" />
        <td style="width: 150px">Country:<br />
            <asp:TextBox ID="txtCountry" runat="server"Width="140px" />
        <td style="width: 200px">
            <br />
            <asp:Button Text="Update" runat="server" OnClick="OnSubmit" />


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

Updating the record in Database using Stored Procedure in ASP.Net

When the Update 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 UPDATE query is passed to it as parameter.
The values of the CustomerId, Name and Country are added as parameter to SqlCommand object.
And the connection is opened and the ExecuteNonQuery function is executed.
Note: For more details on how to use ExecuteNonQuery function, please refer Understanding SqlCommand ExecuteNonQuery in C# and VB.Net.
Finally, the connection is closed and based on whether record is updated or not, an appropriate message displayed in JavaScript Alert Message Box using RegisterStartupScript method.
protected void OnSubmit(object sender, EventArgs e)
    string spName = "Customers_UpdateCustomer";
    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("@CustomerId", txtId.Text);
            cmd.Parameters.AddWithValue("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
            cmd.Connection = con;
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer record updated.');", true);
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer not found.');", true);
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
    Dim spName As String = "Customers_UpdateCustomer"
    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("@CustomerId", txtId.Text)
            cmd.Parameters.AddWithValue("@Name", txtName.Text)
            cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
            cmd.Connection = con
            Dim i As Integer = cmd.ExecuteNonQuery()
            If i > 0 Then
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer record updated.');", True)
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer not found.');", True)
            End If
        End Using
    End Using
End Sub


Calling Update SQL Server Stored Procedures using ADO.Net