Update command in Datagrid
Hi all,
I'm new to this group.
My update command of Datagrid doesn't work
here is the coding:
-------------------------------------------------------------
public class display : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
RegBLLCom objBLL;
private string OrderBy
{
get
{
return ViewState["ORDERBY"] != null ? ViewState["ORDERBY"].ToString():"ASC";
}
set
{
ViewState["ORDERBY"] = value;
}
}
private void makeConnection()
{
objBLL = new RegBLLCom(Config.connect2);
}
void Page_Load(object sender, System.EventArgs e)
{
makeConnection();
DataGrid1.DataSource=objBLL.PopulateGrid();;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dg_cancel);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dg_edit);
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEvent Handler(this.gd_sort);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dg_Update);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dg_edit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex =(int) e.Item.ItemIndex;
DataGrid1.DataBind();
}
private void dg_cancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
private void dg_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//makeConnection();
SqlConnection con = new SqlConnection(Config.connect2);
string updateCement = "Update sample SET invid=@invid,itemno=@itemno,itemname=@itemname,qua ntity=@quantity WHERE invid=@invid";
SqlCommand com = new SqlCommand(updateCement,con);
com.Parameters.Add(new SqlParameter("@invid",SqlDbType.VarChar,50));
com.Parameters.Add(new SqlParameter("@itemno",SqlDbType.VarChar,50));
com.Parameters.Add(new SqlParameter("@itemname",SqlDbType.VarChar,50));
com.Parameters.Add(new SqlParameter("@quantity",SqlDbType.NVarChar,50));
com.Parameters["@invid"].Value = DataGrid1.DataKeys[(int) e.Item.ItemIndex];
com.Parameters["@itemno"].Value = ((TextBox) e.Item.Cells[2].Controls[0]).Text;
com.Parameters["@itemname"].Value = ((TextBox) e.Item.Cells[3].Controls[0]).Text;
com.Parameters["@quantity"].Value = ((TextBox) e.Item.Cells[4].Controls[0]).Text;
try
{
com.Connection.Open();
com.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
DataGrid1.DataSource=objBLL.PopulateGrid();
DataGrid1.DataBind();
con.Close();
}
catch(Exception x)
{
throw x;
}
}
}
---------------------------------------------------------------
//HTML coding
<%@ Page language="c#" Codebehind="display.aspx.cs" AutoEventWireup="false" Inherits="RND.Registration.display" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>display</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table height="200" cellSpacing="0" cellPadding="0" width="300" align="center" border="0">
<tr vAlign="middle" width="100%">
<td vAlign="middle" align="center" width="50%">
<asp:datagrid DataKeyField=invid id="DataGrid1" runat="server" Height="161px" Width="251px" AllowSorting="True">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid></td>
</tr>
</table>
</form>
</body>
</HTML>
------------------------------------------------------------------------------------------
Can anyone tell me where's the problem ?
Kindly do respond ASAP.
Thanks,
with warm regards,
S.Chandra Mohan
Whatever happens, happens for good.
|