My Code is : using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Drawing; using System.Data; public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection(@"Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Employee_Details;Integrated Security=True"); private int result; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindEmployeeDetails(); } } private void BindEmployeeDetails() { con.Open(); SqlCommand cmd = new SqlCommand("Select * from Employees_Details", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvDetails.DataSource = ds; gvDetails.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvDetails.DataSource = ds; gvDetails.DataBind(); int coloumcount = gvDetails.Rows[0].Cells.Count; gvDetails.Rows[0].Cells.Clear(); gvDetails.Rows[0].Cells.Add(new TableCell()); gvDetails.Rows[0].Cells[0].ColumnSpan = coloumcount; gvDetails.Rows[0].Cells[0].Text = "No Record Found"; } } protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvDetails.EditIndex = -1; BindEmployeeDetails(); } protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("AddNew")) { TextBox txtUserName = (TextBox)gvDetails.FooterRow.FindControl("txtUserName"); TextBox txtCity = (TextBox)gvDetails.FooterRow.FindControl("txtCity"); TextBox txtdesignation = (TextBox)gvDetails.FooterRow.FindControl("txtdesignation"); con.Open(); if (txtCity.Text == "" || txtdesignation.Text == "" || txtUserName.Text == "") { // Response.Write(@"<Script language='JavaScript'> alert('Enter The Details Properly');void ('');</script">); string textForMessage = @"<script language='javascript'> alert('Enter The Complete Details');</script>"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UserPopup", textForMessage); } else { SqlCommand cmd = new SqlCommand("Insert into Employees_Details(UserName,City,Designation) values('" + txtUserName.Text + "','" + txtCity.Text + "','" + txtdesignation.Text + "')", con); int result = cmd.ExecuteNonQuery(); con.Close(); if (result == 1) { BindEmployeeDetails(); LblResult.ForeColor = Color.Green; LblResult.Text = txtUserName.Text + "Details Inserted Successfully"; } else { LblResult.ForeColor = Color.Red; LblResult.Text = txtUserName.Text + "Details Not Inserted"; } } } } protected void gvDetails_RowDeleted(object sender, GridViewDeletedEventArgs e) { } protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e) { int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString()); string UserName = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString(); con.Open(); SqlCommand cmd = new SqlCommand("Delete From Employees_Details where UserId= +UserId", con); con.Close(); if (result == 1) { BindEmployeeDetails(); LblResult.ForeColor = Color.Red; LblResult.Text = UserName + "Details Deleted Successfully"; } } protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e) { gvDetails.EditIndex = e.NewEditIndex; BindEmployeeDetails(); } protected void gvDetails_RowUpdated(object sender, GridViewUpdatedEventArgs e) { } protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e) { int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString()); string UserName = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString(); TextBox txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtcity"); TextBox txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDesigantion"); con.Open(); SqlCommand cmd = new SqlCommand("update Employees_Details set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where UserId=" + UserId, con); cmd.ExecuteNonQuery(); con.Close(); LblResult.ForeColor = Color.Green; LblResult.Text = UserName + "Details Updated Successfully"; gvDetails.EditIndex = -1; BindEmployeeDetails(); } protected void gvDetails_RowCreated(object sender, GridViewRowEventArgs e) { } protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e) { } protected void gvDetails_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { } }
int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)