无法将“System.Web.UI.WebControls.TextBox”类型的对象强制转换为“System.IConvertible”类型 [英] Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'

查看:72
本文介绍了无法将“System.Web.UI.WebControls.TextBox”类型的对象强制转换为“System.IConvertible”类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新我的gridview但是我收到了这个错误。

无法将'System.Web.UI.WebControls.TextBox'类型的对象强制转换为'System.IConvertible'



这里我的aspx代码:



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>
                <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>







我的cs代码:




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


这篇关于无法将“System.Web.UI.WebControls.TextBox”类型的对象强制转换为“System.IConvertible”类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆