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'
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"
));
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 think first you have create obejct of textbox then get Text from textbox object and convert it to int or whatever
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.
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