Delete record using enterprise library in Asp.Net

InfoPath alternatives for form designing SharePoint

In this article we will discuss how to delete a record using enterprise library in Asp.Net.

You should read how to use enterprise library in Asp.Net before starting this article.

You can also check my previous posts on

Null Coalescing Operator in C#.Net

Set default focus in Asp.net

Method Overloading in C#.Net.

In this post we will see, whenever a user put an id in the textbox and click on submit, the data should get deleted from the database. Below is the full code:

Store Procedure:

CREATE PROCEDURE DeleteEmployeData
@Empid int
AS
BEGIN

DELETE FROM EmloyeeDetail WHERE Empid=@Empid

END
GO

.aspx code:

<table>
<tr>
<td>
Emp Id
</td>
<td>
<asp:TextBox ID=”txtEmpid” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID=”btnDelete” runat=”server” Text=”Delete” OnClick=”btnDelete_Click” />
</td>
</tr>
<asp:Label ID=”lblResult” runat=”server” Text=”Label”></asp:Label>
</table>

.aspx.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data;


public partial class DeleteData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDelete_Click(object sender, EventArgs e)
{
Database db = DatabaseFactory.CreateDatabase(“connection”);
DbCommand dbCommand = db.GetStoredProcCommand(“[DeleteEmployeData]”);
db.AddInParameter(dbCommand, “@Empid”, DbType.Int32, Convert.ToInt32(txtEmpid.Text));

try
{
int result = db.ExecuteNonQuery(dbCommand);
if (result > 0)
{
lblResult.Text = “Record deleted successfully!”;
}
else
{
lblResult.Text = “Some error occured!”;
}
}
catch (Exception ex)
{
}
}
}


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply