0

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>
Max Play
  • 3,717
  • 1
  • 22
  • 39
  • put a break point before ExecuteNonQuery and see if the value in parameter are what you expected. If they are then try to run it in SSMS and see if you are getting the result you expect. If not then your query arent right – Steve Feb 26 '19 at 18:03
  • use try & catch to know get more info about the issue. – Prabin Yovan Feb 26 '19 at 18:13
  • For one thing you are passing in string values to your parameters and based on the parameter names chances are they are not all defined as strings in the database schema. You should parse some of those strings first and also validate the values. Example: `price` is probably a decimal so convert it to a decimal in c# and pass that as the parameter value. Using `AddWithValue` is also bad practice because you can't specify the columns defined type. Use `Add` instead. – Igor Feb 26 '19 at 18:33
  • See also [How can I add user-supplied input to an SQL statement?](https://stackoverflow.com/q/35163361/1260204) – Igor Feb 26 '19 at 18:34
  • Check your query while debugging the code and also see if qrcode is proper and as expected. Also why don't you use try-catch to comprehend if you're getting exception / check the o/p of ExecuteNonQuery(). – JJBHATT Feb 26 '19 at 18:51
  • Thanks for the replies, will use try and catch and post the results – MightyZ potatoZ Feb 26 '19 at 18:53

0 Answers0