Hi I'm working on building a webpage that display a table from SQL database and giving the use the option to updated the fields.
I was able to pull up the list and display it but I'm not being able to update any of the fields. When I try to updated any field it just revert back as soo I hit update.
Tried to trace the issue but not luck as I'm not sure why the sql command not running.
The Question is what am I doing wrong that the sql command not running?
Here is my update function.
protected void tabledisplay_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = tabledisplay.Rows[e.RowIndex];
int qrcode = Convert.ToInt32(tabledisplay.DataKeys[e.RowIndex].Values[0]);
string itemname = (row.Cells[2].Controls[0] as TextBox).Text;
string itemtype = (row.Cells[3].Controls[0] as TextBox).Text;
string price = (row.Cells[4].Controls[0] as TextBox).Text;
SqlConnection con = new SqlConnection(connectionString)
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "UPDATE InvMangPallets SET itemname = @itemname, itemtype = @itemtype, price = @price WHERE qrcode = @qrcode";
cmd.Parameters.AddWithValue("@qrcode", qrcode);
cmd.Parameters.AddWithValue("@itemname", itemname);
cmd.Parameters.AddWithValue("@itemtype", itemtype);
cmd.Parameters.AddWithValue("@price", price);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
//con.Close();
}
this.DataBind();
}
Here is the asp-code:
<asp:GridView ID="tabledisplay" runat="server" DataKeyNames="qrcode" AutoGenerateColumns="false" HeaderStyle-Font-Bold="true"
OnRowEditing="tabledisplay_RowEditing" OnRowCancelingEdit="tabledisplay_RowCancelingEdit" OnRowUpdating="tabledisplay_RowUpdating"
AutoGenerateEditButton="true">
<Columns>
<asp:BoundField DataField="qrcode" HeaderText="QR Code" />
<asp:BoundField DataField="itemname" HeaderText="Item Name" />
<asp:BoundField DataField="itemtype" HeaderText="Item Type" />
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:BoundField DataField="date" HeaderText="Date" />
</Columns>
</asp:GridView>