相关文章推荐
完美的莴苣  ·  ASP.NET Core Blazor ...·  1 月前    · 
聪明伶俐的馒头  ·  win ...·  6 月前    · 
任性的冰淇淋  ·  小栗帽漫画 - 抖音·  1 年前    · 
知识渊博的投影仪  ·  电影在线观看·  1 年前    · 
天涯  ·  Python+selenium设置随机IP代 ...·  1 年前    · 
hi, I wan to update my gridview but I got this error.
Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'
here my aspx code :
< 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 > < footer style 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 >
my cs code :
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();
I've got the wrong PC with me so have the wrong VisStudio :-(
Try breaking the line down into its parts e.g.
GridViewRow dr = GridView1.Rows[e.RowIndex];
TextBox tb = (TextBox)dr.FindControl("TextBox4");
int pCategoryID = Convert.ToInt32(tb.Text);
(syntax might need to be corrected). Then debug to check what's available at each stage - e.g. make sure it's found textbox4 etc
I think first you have create obejct of textbox then get Text from textbox object and convert it to int or whatever
C#
TextBox CategoryID = (TextBox)GridView1.Rows[e.RowIndex].FindControl( " TextBox4" ); int pCategoryId = Convert.ToInt32(CategoryID.Text);
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •