Update record using Enterprise library in Asp.Net

In this post we will discuss about how a user can update a record using enterprise library in Asp.Net. To know more how to work with enterprise library in asp.net, you can read this article before starting this.

In this post we will see,
There will be one textbox where a user can put a ID and corresponding records will be showed in the corresponding textboxes. Then user can update the record and finally the data will be saved in the database.

Below is the code:

Stored Procedure:
Here are the two stored procedure used:

create procedure SelectEmployee
@Empid int
as
begin
Select * from EmloyeeDetail where Empid=@Empid
end
go
ok

create procedure UpdateEmployeeData
@FirstName varchar(100),
@LastName varchar(100),
@Address varchar(100),
@ContactNo varchar(20),
@Empid int
as
begin
update EmloyeeDetail
set
FirstName=@FirstName,
LastName=@LastName,
Address=@Address,
ContactNo=@ContactNo
where
Empid=@Empid
end
go
ok

.aspx code:

<div>
<table>
<tr><td>Emp Id</td><td><asp:TextBox ID=”txtId” runat=”server”></asp:TextBox></td></tr>

<tr><td><asp:Button ID=”btnEdit” runat=”server” Text=”Edit”
onclick=”btnEdit_Click” />

</td></tr>
<tr><td>First Name</td><td><asp:TextBox ID=”txtFirstName” runat=”server”></asp:TextBox></td></tr>
<tr><td>Last NAme</td><td><asp:TextBox ID=”txtLastName” runat=”server”></asp:TextBox></td></tr>
<tr><td>Address</td><td><asp:TextBox ID=”txtAddress” runat=”server”></asp:TextBox></td></tr>
<tr><td>Contact No</td><td><asp:TextBox ID=”txtContactNo” runat=”server”></asp:TextBox></td></tr>
<tr><td>
<asp:Button ID=”btnUpdate” runat=”server” Text=”Update” onclick=”btnUpdate_Click” /></td></tr>
</table>
</div>

.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 SpSampleTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnEdit_Click(object sender, EventArgs e)
{
Database db = DatabaseFactory.CreateDatabase(“connection”);
DbCommand dbCommand = db.GetStoredProcCommand(“[selectEmployee]”);
db.AddInParameter(dbCommand, “@Empid”, DbType.Int32, Convert.ToInt32(txtId.Text));
DataSet dataSet = db.ExecuteDataSet(dbCommand);
if (dataSet != null)
{
if (dataSet.Tables.Count >0)
{
txtFirstName.Text = dataSet.Tables[0].Rows[0][“FirstName”].ToString();
txtLastName.Text=dataSet.Tables[0].Rows[0][“LastName”].ToString();
txtAddress.Text = dataSet.Tables[0].Rows[0][“Address”].ToString();
txtContactNo.Text = dataSet.Tables[0].Rows[0][“ContactNo”].ToString();
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
Database db = DatabaseFactory.CreateDatabase(“connection”);
DbCommand dbCommand = db.GetStoredProcCommand(“[UpdateEmployeeData]”);
db.AddInParameter(dbCommand, “@Empid”, DbType.Int32, Convert.ToInt32(txtId.Text));
db.AddInParameter(dbCommand, “@FirstName”, DbType.AnsiString, txtFirstName.Text);
db.AddInParameter(dbCommand, “@LastName”, DbType.AnsiString, txtLastName.Text);
db.AddInParameter(dbCommand, “@Address”, DbType.AnsiString, txtAddress.Text);
db.AddInParameter(dbCommand, “@ContactNo”, DbType.AnsiString, txtContactNo.Text.ToString());
db.ExecuteNonQuery(dbCommand);
}
}


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