Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
<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>



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();
        }
    }
Posted
Updated 22-Jun-13 6:54am
v2
Comments
NooobieCoder 22-Jun-13 12:57pm    
@CHill60 - thanks for helping me make it look nicer!
CHill60 22-Jun-13 12:58pm    
That's ok ... I have a suggestion coming up on how to help you work out the problem ... I'll be back!
NooobieCoder 22-Jun-13 12:59pm    
ok thanks!
CHill60 22-Jun-13 13:06pm    
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
NooobieCoder 22-Jun-13 13:56pm    
alright now everything is working up till this line
cmd.ExecuteNonQuery();

Incorrect syntax near '='

1 solution

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);
 
Share this answer
 
Comments
NooobieCoder 22-Jun-13 13:38pm    
hmm still the same error
CHill60 22-Jun-13 13:54pm    
Are you getting the error on the first or second line now? (*wishing I had VisStudio to check this*!)
NooobieCoder 22-Jun-13 14:04pm    
alright now everything is working up till this line
cmd.ExecuteNonQuery();

Incorrect syntax near '='

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900