In this article I will explain with an example, how to
Delete data from database using
Stored Procedure in ASP.Net using C# and VB.Net.
ADO.Net will be used to perform
Delete operation in ASP.Net.
Database
I have made use of the following table Customers with the schema as follows.
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
CustomerId parameter, which is used to DELETE the records in
Customers Table.
CREATE PROCEDURE [dbo].[Customers_DeleteCustomer]
@CustomerId INT
AS
BEGIN
DELETE FROM [Customers]
WHERE CustomerId = @CustomerId
END
HTML Markup
The HTML Markup consists of:
TextBox – For entering Id.
Button – Submitting the Form.
The Button has been assigned 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: 200px">
<br />
<asp:Button Text="Delete" runat="server" OnClick="OnSubmit" />
</td>
</tr>
</table>
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Deleting the record from Database using Stored Procedure in ASP.Net
When the Delete 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.
Then, an object of SqlCommand class is created and the DELETE query is passed to it as parameter.
The value of the CustomerId is added as parameter to SqlCommand object.
And the connection is opened and the ExecuteNonQuery function is executed.
Finally, the connection is closed and based on whether record is deleted or not, an appropriate message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
C#
protected void OnSubmit(object sender, EventArgse)
{
string spName = "Customers_DeleteCustomer";
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.Connection = con;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer record deleted.');", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer not found.');", true);
}
}
}
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim spName As String = "Customers_DeleteCustomer"
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.Connection = con
con.Open()
Dim i As Integer = cmd.ExecuteNonQuery()
con.Close()
If i > 0 Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer record deleted.');", True)
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer not found.');", True)
End If
End Using
End Using
End Sub
Screenshot
Downloads