<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="productID" DataSourceID="ObjectDataSource1" onrowdeleting="GridView1_RowDeleting" onrowupdating="GridView1_RowUpdating" ShowFooter="True"> <columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> <asp:TemplateField HeaderText="productID" InsertVisible="False" SortExpression="productID"> <edititemtemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("productID") %>'> </edititemtemplate> <footertemplate> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" /> </footertemplate> <itemtemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("productID") %>'> </itemtemplate> <footerstyle wrap="True" /> <asp:TemplateField HeaderText="productName" SortExpression="productName"> <edititemtemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("productName") %>'> </edititemtemplate> <footertemplate> <asp:TextBox ID="TextBox7" runat="server"> </footertemplate> <itemtemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("productName") %>'> </itemtemplate> <asp:TemplateField HeaderText="productDescription" SortExpression="productDescription"> <edititemtemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("productDescription") %>'> </edititemtemplate> <footertemplate> <asp:TextBox ID="TextBox8" runat="server"> </footertemplate> <itemtemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("productDescription") %>'> </itemtemplate> <asp:TemplateField HeaderText="productImg" SortExpression="productImg"> <edititemtemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("productImg") %>'> </edititemtemplate> <itemtemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("productImg") %>'> </itemtemplate> <asp:TemplateField HeaderText="pCategoryID" SortExpression="pCategoryID"> <edititemtemplate> <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pCategoryID") %>'> </edititemtemplate> <footertemplate> <asp:DropDownList ID="DropDownList1" runat="server"> </footertemplate> <itemtemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("pCategoryID") %>'> </itemtemplate> <asp:TemplateField HeaderText="productPrice" SortExpression="productPrice"> <edititemtemplate> <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("productPrice") %>'> </edititemtemplate> <footertemplate> <asp:TextBox ID="TextBox9" runat="server"> </footertemplate> <itemtemplate> <asp:Label ID="Label6" runat="server" Text='<%# Bind("productPrice") %>'> </itemtemplate> <asp:TemplateField HeaderText="stockQuantity" SortExpression="stockQuantity"> <edititemtemplate> <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("stockQuantity") %>'> </edititemtemplate> <footertemplate> <asp:TextBox ID="TextBox10" runat="server"> </footertemplate> <itemtemplate> <asp:Label ID="Label7" runat="server" Text='<%# Bind("stockQuantity") %>'> </itemtemplate> </columns>
int ProductID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); String productName = Convert.ToString((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")); String productDesc = Convert.ToString((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")); String productImg = Convert.ToString((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")); int pCategoryID = Convert.ToInt32((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4")); // the error occur in this line String productPriceS = Convert.ToString((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5")); String stockQuantityS = Convert.ToString((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6")); int pCategoryIDS = Convert.ToInt32(pCategoryID); int productPrice = Convert.ToInt32(productPriceS); int stockQuantity = Convert.ToInt32(stockQuantityS); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DKGWConnectionString"].ConnectionString)) { string sql = ("UPDATE BQ_Product" + "productName=@productName, productDescription=@productDescription, productImg=@productImg, pCategoryID=@pCategoryID, productPrice=@productPrice, stockQuantity=@stockQuantity" + "WHERE productID=@productID"); using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@productName", productName); cmd.Parameters.AddWithValue("@productDescription", productDesc); cmd.Parameters.AddWithValue("@productImg", productImg); cmd.Parameters.AddWithValue("@pCategoryID", pCategoryID); cmd.Parameters.AddWithValue("@productPrice", productPrice); cmd.Parameters.AddWithValue("@stockQuantity", stockQuantity); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; } } GridView1.DataBind(); } }
string sql
TextBox CategoryID = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4"); int pCategoryId = Convert.ToInt32(CategoryID.Text);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)