using
System;
using
System.Data;
using
System.Data.SqlClient;
using
System.Web.UI.WebControls;
public
partial class
_Default : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindEmployeeDeatil();
}
}
private
void BindEmployeeDeatil()
{
SqlConnection con = new
SqlConnection("Data
Source=.;Persist Security Info=True;User ID=sa;Password=pramod");
con.Open();
SqlCommand cmd = new
SqlCommand("Usp_userdtl",
con);
cmd.CommandType =
CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@mode",
"S");
SqlDataAdapter da = new
SqlDataAdapter(cmd);
DataSet ds = new
DataSet();
da.Fill(ds);
if
(ds != null && ds.Tables.Count > 0)
{
gvempdtl.DataSource =
ds.Tables[0];
gvempdtl.DataBind();
}
}
private
void Reset()
{
hdnid.Value =
string.Empty;
txtname.Text =
string.Empty;
txtdept.Text =
string.Empty;
txtAge.Text =
string.Empty;
btnSave.Text =
"Save";
BindEmployeeDeatil();
}
protected void btnSave_Click(object
sender, EventArgs e)
{
try
{
lblSuccessmsg.Text =
string.Empty;
lblErrormsg.Text =
string.Empty;
string mode, Id = string.Empty;
if (string.IsNullOrEmpty(hdnid.Value))
{
mode =
"I";
Id =
"0";
}
else
{
mode =
"U";
Id = hdnid.Value;
}
SqlConnection con =
new
SqlConnection("Data Source=.;Persist
Security Info=True;User ID=sa;Password=pramod");
con.Open();
SqlCommand cmd =
new
SqlCommand("Usp_userdtl",
con);
cmd.CommandType =
CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@mode",
mode);
cmd.Parameters.AddWithValue("@Id",
Id);
cmd.Parameters.AddWithValue("@Name",
txtname.Text);
cmd.Parameters.AddWithValue("@Age",
txtAge.Text);
cmd.Parameters.AddWithValue("@Department",
txtdept.Text);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
if (btnSave.Text.Trim() ==
"Save")
{
lblSuccessmsg.Text = "Saved Successfully";
}
else if
(btnSave.Text.Trim() == "Update")
{
lblSuccessmsg.Text = "Updated Successfully";
}
Reset();
}
else
{
lblErrormsg.Text =
"Please Contact to Administrator";
}
}
catch (Exception ex)
{
lblErrormsg.Text =
ex.Message;
}
}
protected void btnEdit_Click(object
sender, EventArgs e)
{
try
{
lblSuccessmsg.Text =
string.Empty;
lblErrormsg.Text =
string.Empty;
Button btn = sender
as Button;
GridViewRow row = (GridViewRow)btn.NamingContainer;
hdnid.Value =
(row.FindControl("lblId")
as Label).Text;
txtname.Text =
(row.FindControl("lblName")
as Label).Text;
txtAge.Text =
(row.FindControl("lblAge")
as Label).Text;
txtdept.Text =
(row.FindControl("lblDepartment")
as Label).Text;
btnSave.Text =
"Update";
}
catch (Exception ex)
{
lblErrormsg.Text =
ex.Message;
}
}
protected void btnDelete_Click(object
sender, EventArgs e)
{
try
{
lblSuccessmsg.Text =
string.Empty;
lblErrormsg.Text =
string.Empty;
Button btn = sender
as Button;
GridViewRow row = (GridViewRow)btn.NamingContainer;
SqlConnection con =
new
SqlConnection("Data Source=.;Persist
Security Info=True;User ID=sa;Password=pramod");
con.Open();
SqlCommand cmd =
new
SqlCommand("Usp_userdtl",
con);
cmd.CommandType =
CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@mode",
"D");
cmd.Parameters.AddWithValue("@Id", (row.FindControl("lblId")
asLabel).Text);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
lblSuccessmsg.Text =
"Deleted Successfully";
Reset();
}
else {
lblErrormsg.Text =
"Please Contact to Administrator";
}
}
catch (Exception ex)
{
lblErrormsg.Text =
ex.Message;
}
}
}
|