将 varchar 值 'System.Web.UI.WebControls.TextBox' 转换为数据类型 int 时转换失败

Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int

我想更新我的 gridview 中的数据。但是,当我尝试更新它时它向我显示此错误。这是我的 aspx 代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" 
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" 
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
        OnRowUpdating="GridView1_RowUpdating">
<Columns>
    <asp:TemplateField HeaderText="ProductName">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtboxProductName" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
             ErrorMessage="Invalid" ForeColor="Red" ControlToValidate="txtboxProductName"
            ValidationExpression="^[a-zA-Z ]+$"></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProductDescription">
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtboxProductDescription" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Invalid" 
            ForeColor="Red" ControlToValidate="txtboxProductDescription"
            ValidationExpression="^[a-zA-Z ]+$"></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:ImageField HeaderText ="ProductImage" DataImageUrlField="ProductImage" SortExpression="ProductImage" ControlStyle-Width ="10">

        <ControlStyle Width="50px"></ControlStyle>

        </asp:ImageField>
    <asp:TemplateField HeaderText="ProductQuantity">
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductQuantity") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtboxProductQuant" runat="server" Text='<%# Bind("ProductQuantity") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Invalid" ControlToValidate="txtboxProductQuant"
            ForeColor="Red" ValidationExpression=^[0-9]*$></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProductPrice">
        <ItemTemplate>
            <asp:Label ID="Label4" runat="server" Text='<%# Bind("ProductPrice") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtboxProductPrice" runat="server" Text='<%# Bind("ProductPrice") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Invalid" ControlToValidate="txtboxProductPrice"
            ForeColor="Red" ValidationExpression=^[0-9]*$></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="true" />
    <%--<asp:CommandField ShowDeleteButton="true" />--%>
    <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete" 
        OnClientClick="return confirm('Confirm Delete?');"></asp:LinkButton>

        </ItemTemplate>
        <ItemStyle Width="100px" />

        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我的更新代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Finding the controls from Gridview for the row which is going to update  
        //Label id = GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
        int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox ProductName = GridView1.Rows[e.RowIndex].FindControl("txtboxProductName") as TextBox;
        TextBox ProductDescription = GridView1.Rows[e.RowIndex].FindControl("txtboxProductDescription") as TextBox;
        TextBox ProductQuantity = GridView1.Rows[e.RowIndex].FindControl("txtboxProductQuant") as TextBox;
        TextBox ProductPrice = GridView1.Rows[e.RowIndex].FindControl("txtboxProductPrice") as TextBox;
        conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=Authorship;Integrated Security =True");
        conn.Open();
        //updating the record  
        SqlCommand cmd = new SqlCommand("Update Products set ProductName='" + ProductName.Text + "',ProductDescription='" + ProductDescription.Text + "',ProductQuantity='" + ProductQuantity.Text + "', ProductPrice='" + ProductPrice + "' where ProductID='" + userid + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
        GridView1.EditIndex = -1;
        //Call ShowData method for displaying updated data  
        gvbind(); 
}

错误特别出现在文本框 productprice 中,因为当我尝试创建 FindControl("") 时,它可以正常工作,但 returns 0 作为值。但其他数据工作正常。当我插入 findcontrol(txtboxProductPrice) 时,它显示错误:

Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.

我的 table 包含

ProductID - int
ProdcuctName - varchar(500)
ProductDescription - varchar(500)
ProductImage - varchar(500)
ProductQuantity - int
ProductPrice - int

在您的查询中,您有部分代码如下:

+ "', ProductPrice='" + ProductPrice + "' where ProductID='" +

ProductPrice 是一个文本框,因此您需要使用 属性 Text 来访问文本:

+ "', ProductPrice='" + ProductPrice.Text + "' where ProductID='" +

此外,非常重要的注意事项:永远不要像您那样通过连接命令来创建查询,因为您很容易受到 SQL Injection attack 的攻击。网上有成千上万篇关于如何解决此问题的文章,请务必阅读它们并以正确的方式实施您的查询。

SqlCommand cmd = new SqlCommand("Update Products set ProductName='" + ProductName.Text + "',ProductDescription='" + ProductDescription.Text + "',ProductQuantity='" + ProductQuantity.Text + "', ProductPrice.Text='" + ProductPrice + "' where ProductID='" + userid + "'", conn);

使用 ProductPrice.Text 而不是 ProductPrice