using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
using
System.Configuration;
public
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
public void BindData()
{
string constring = ConfigurationManager.AppSettings["connection"];
SqlConnection con = new SqlConnection(constring);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_display1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds,
"Employer");
gvDisplay.DataSource = ds;
gvDisplay.DataBind();
con.Close();
//First we make a SqlCommand object and name it myCommand. The SqlCommand object takes a stored procedure as an input and the SqlConnection.
//We feed the command object to the DataAdapter object named as myAdapter.
//A dataset is declared which is filled with the result of the Stored procedure.
//myDataGrid.DataBind() binds the datagrid to the page. Don't forget to bind the grid or else it won't be displayed.
//Later we opened the connection and execute the query.
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
public string GetData(object o)
{
string str = Convert.ToString(o);
if ((str == null) || (str == string.Empty) || (str == "-1.00"))
return "-";
else
return str.Trim();
}
//To Edit we can call this function in the OnRowEditing event of Gridview
public void Edit_DataGrid(object source, GridViewEditEventArgs e)
{
// We use CommandEventArgs e to get the row which is being clicked
// This also changes the DataGrid labels into Textboxes so user can edit them
gvDisplay.EditIndex = e.NewEditIndex;
// Always bind the data so the datagrid can be displayed.
BindData();
}
//When you change your mind about editing you can press cancel link and it is called in OnRowCancelingEdit event
public void Cancel_DataGrid(object source, GridViewCancelEditEventArgs e)
{
// All we do in the cancel method is to assign '-1' to the datagrid EditIndex
// Once the EditIndex is set to '-1' the datagrid returns back to its original condition
gvDisplay.EditIndex = -1;
BindData();
}
//When we Update Gridview to reflect those changes into DataSet we will call this function into OnRowUpdating event
public void Update_DataGrid(object source, GridViewUpdateEventArgs e)
{
string ID="", Name="", Designation="", Department="", Salary="";
DataControlFieldCell IDcell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[0];
gvDisplay.Columns[0].ExtractValuesFromCell(e.NewValues, IDcell,
DataControlRowState.Edit, true);
DataControlFieldCell Namecell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[1];
gvDisplay.Columns[1].ExtractValuesFromCell(e.NewValues, Namecell,
DataControlRowState.Edit, true);
DataControlFieldCell Departmentcell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[2];
gvDisplay.Columns[2].ExtractValuesFromCell(e.NewValues, Departmentcell,
DataControlRowState.Edit, true);
DataControlFieldCell Designationcell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[3];
gvDisplay.Columns[3].ExtractValuesFromCell(e.NewValues, Designationcell,
DataControlRowState.Edit, true);
DataControlFieldCell Salarycell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[4];
gvDisplay.Columns[4].ExtractValuesFromCell(e.NewValues, Salarycell,
DataControlRowState.Edit, true);
foreach(string key in e.NewValues.Keys)
{
switch (key)
{
case "EmpID":
ID = e.NewValues[key].ToString();
break;
case "EmpName":
Name = e.NewValues[key].ToString();
break;
case "Department":
Department = e.NewValues[key].ToString();
break;
case "Designation":
Designation = e.NewValues[key].ToString();
break;
case "Salary":
Salary = e.NewValues[key].ToString();
break;
}
}
string id=ID,name=Name,department=Department,designation=Designation,salary=Salary;
//Not Executing Code
////We declare a variable of TextBox type and call it cEmpID. The reason of declaring a TextBox is that the
////value that we want is inside the TextBox which is inside the GridView control.
////TextBox cEmpID = new TextBox();
////cEmpID = (TextBox)gvDisplay.Rows[e.RowIndex].Cells[0].Controls[0];
////gvDisplay.Columns[0].ExtractValuesFromCell(e.NewValues, cEmpID, DataControlRowState.Edit, true);
////string idcell = gvDisplay.Columns[0].ExtractValuesFromCell(
//DataControlFieldCell IDcell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[0];
//gvDisplay.Columns[0].ExtractValuesFromCell((System.Collections.Specialized.IOrderedDictionary)e.NewValues[0], IDcell, DataControlRowState.Edit, true);
//string x = e.NewValues[0].ToString();
////DataControlFieldCell Namecell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[0];
////gvDisplay.Columns[0].ExtractValuesFromCell(e.NewValues, Namecell, DataControlRowState.Edit, true);
////DataControlFieldCell Departmentcell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[0];
////gvDisplay.Columns[0].ExtractValuesFromCell(e.NewValues, Departmentcell, DataControlRowState.Edit, true);
////DataControlFieldCell Designationcell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[0];
////gvDisplay.Columns[0].ExtractValuesFromCell(e.NewValues, Designationcell, DataControlRowState.Edit, true);
////DataControlFieldCell Salarycell = (DataControlFieldCell)gvDisplay.Rows[e.RowIndex].Cells[0];
////gvDisplay.Columns[0].ExtractValuesFromCell(e.NewValues, Salarycell, DataControlRowState.Edit, true);
////cName = (System.Web.UI.WebControls.TextBox) e.Item.Cells[1].Controls[0];
////Later we made the SqlCommand object which takes stored procedure sp_update2
////create procedure sp_update2 ( @EmpID int,@EmpName varchar(50),@Department varchar(50),@Designation varchar(50), @Salary decimal(12,2))
////as Begin
////update Employer set EmpID=@EmpID,EmpName=@EmpName,Department=@Department,Designation=@Designation,Salary=@Salary Where EmpID=@EmpID
////END;
string constring = ConfigurationManager.AppSettings["connection"];
SqlConnection con = new SqlConnection(constring);
SqlCommand myCommand = new SqlCommand("sp_update2", con);
myCommand.CommandType =
CommandType.StoredProcedure;
////After marking the command object with the stored procedure we passed the parameter which is PersonName.
myCommand.Parameters.Add(
new SqlParameter("@EmpID", SqlDbType.Int));
//int x = Convert.ToInt32(IDcell.Text);
myCommand.Parameters[
"@EmpID"].Value = ID;
myCommand.Parameters.Add(
new SqlParameter("@EmpName", SqlDbType.VarChar, 50));
myCommand.Parameters[
"@EmpName"].Value = Name;
myCommand.Parameters.Add(
new SqlParameter("@Department", SqlDbType.VarChar, 50));
myCommand.Parameters[
"@Department"].Value = Department;
myCommand.Parameters.Add(
new SqlParameter("@Designation", SqlDbType.VarChar, 50));
myCommand.Parameters[
"@Designation"].Value = Designation;
myCommand.Parameters.Add(
new SqlParameter("@Salary", SqlDbType.Decimal, 18));
myCommand.Parameters[
"@Salary"].Value = Salary;
con.Open();
//Finally we execute the Query and set the editItemIndex property of the DataGrid '-1' which will bring
//the GridView back to its original form i.e without any textboxes.
myCommand.ExecuteNonQuery();
con.Close();
gvDisplay.EditIndex = -1;
BindData();
}
// This event is fired when the Select is clicked
public void Select_DataGrid(object sender, System.EventArgs e)
{
// prints the value of the first cell in the DataGrid
Label2.Text += gvDisplay.SelectedRow.Cells.ToString();
}
}
with this code iam not getting updated value in gridview,iam getting previous value only.