In this article I will explain with an example, how to update data into MySQL database with Stored Procedure in ASP.Net using C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Update with Stored Procedure into MySQL Database in ASP.Net
 
I have already inserted few records in the table.
Update with Stored Procedure into MySQL Database in ASP.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 MySQL database table.
This Stored Procedure accepts CustomerId, Name and Country parameters, which are used to UPDATE the records in Customers Table.
DELIMITER //
CREATE PROCEDURE Customers_UpdateCustomer(
    IN CustomerId INT,
    IN Name VARCHAR(100) ,
    IN Country VARCHAR(50)
)
BEGIN
    UPDATE Customers
    SET Name = Name,
        Country = Country
    WHERE Customers.CustomerId = CustomerId;
END//
DELIMITER ;
 
 

HTML Markup

The HTML Markup consists of following controls:
TextBox – For capturing the values of CustomerId, Name and Country.
Button – For updating record.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 60px">Id<br />
            <asp:TextBox ID="txtId" runat="server" Width="50px" />
        </td>
        <td style="width: 150px">Name<br />
            <asp:TextBox ID="txtName" runat="server" Width="140px" />
        </td>
        <td style="width: 150px">Country:<br />
            <asp:TextBox ID="txtCountry" runat="server" Width="140px" />
        </td>
        <td style="width: 200px">
            <br />
            <asp:Button Text="Update" runat="server" OnClick="OnUpdate" />
        </td>
    </tr>
</table>
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
 
 

Updating with Stored Procedure into MySQL Database in ASP.Net

When Update Button is clicked, first the connection is read from Web.Config file.
Note: For more details on how to read connection string from Web.Config file, please refer my article Read or Write Connection Strings in Web.Config file using ASP.Net using C# and VB.Net.
 
The CustomerId, Name and Country fields are fetched from their respective TextBoxes.
Then, using ExecuteNonQuery method record is updated into the MySQL database using Stored Procedure.
Note: For more details on ExecuteNonQuery method, please refer my article Understanding MySqlCommand ExecuteNonQuery in C# and VB.Net.
 
Finally, based on whether record is updated or not an appropriate message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
C#
protected void OnUpdate(object sender, EventArgs e)
{
    string spName = "Customers_UpdateCustomer";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = new MySqlCommand(spName, con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text);
            cmd.Parameters.AddWithValue("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
 
            if (i > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer record updated.');", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer not found.');", true);
            }
        }
    }
}
               
VB.Net
Protected Sub OnUpdate(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 MySqlConnection = New MySqlConnection(constr)
        Using cmd As MySqlCommand = New MySqlCommand(spName, con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text)
            cmd.Parameters.AddWithValue("@Name", txtName.Text)
            cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
            con.Open()
            Dim i As Integer = cmd.ExecuteNonQuery()
            con.Close()
 
            If i > 0 Then
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer record updated.');", True)
            Else
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer not found.');", True)
            End If
        End Using
    End Using
End Sub
 
 

Screenshot

Update with Stored Procedure into MySQL Database in ASP.Net
 
 

Downloads