HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="400px" OnPageIndexChanging="OnPageIndexChanging"
AllowPaging="True" AutoGenerateRows="False" DataKeyNames="EmployeeId" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" OnItemInserting="DetailsView1_ItemInserting"
OnModeChanging="DetailsView1_ModeChanging" OnItemCommand="DetailsViewParentsDetails_ItemCommand"
CellPadding="3" CellSpacing="2" AutoGenerateInsertButton="true" AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true">
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Fields>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
</asp:DetailsView>
</div>
</form>
</body>
</html>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateDetailsView();
}
}
protected void OnPageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
PopulateDetailsView();
}
private void PopulateDetailsView()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM EmployeeRecords", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
conn.Close();
}
}
}
}
protected void DetailsViewParentsDetails_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName.Equals("New"))
{
this.DetailsView1.ChangeMode(DetailsViewMode.Insert);
this.DetailsView1.DataBind();
}
else if (e.CommandName.Equals("Edit"))
{
this.DetailsView1.ChangeMode(DetailsViewMode.Edit);
this.DetailsView1.DataBind();
}
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
PopulateDetailsView();
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string id = (string)e.Values["EmployeeId"];
bool UserExist;
UserExist = this.UserExit(Convert.ToInt32(id));
if (UserExist == false)
{
/*Here you Write Code To Insert User details*/
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('User already exists')", true);
}
}
// Created Method for check User By Name if it Exist or Not which return Boolen value
// If it Exist it Return True
// If not Exist then it Return False
private bool UserExit(int id)
{
string strConnString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("Select * From EmployeeRecords Where EmployeeId =@EmployeeId ");
cmd.Parameters.AddWithValue("@EmployeeId", id);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
SqlDataReader rdr;
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
return true;
}
else
{
return false;
}
con.Close();
}
SQL Table:
CREATE TABLE [dbo].[EmployeeRecords](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[City] [varchar](50) NULL,
[Country] [varchar](50) NULL
) ON [PRIMARY]
GO
Insert query:
INSERT INTO [EmployeeRecords]
([Name]
,[City]
,[Country])
VALUES
('Azim'
,'Mumbai'
,'India')
GO