将varchar值“System.Web.UI.WebControls.TextBox”转换为数据类型int时转换失败 [英] Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int

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

问题描述

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

i want to update data in my gridview. However it shows me this error when i try to update it. here is my aspx code:

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

以下是我的更新代码:

and here is my update code:

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(); 
}

该错误特别在文本框产品价格中,因为当我尝试使FindControl (),其工作但返回0作为一个值。但其他数据工作正常。当我插入findcontrol(txtboxProductPrice)时,它显示我错误:

the error is specifically in the textbox productprice because when i try to make the FindControl(""), its working but returns 0 as a value. but the other data are working fine. and when i insert the findcontrol(txtboxProductPrice), it shows me the error:

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

我的表格包含

my table contains

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


推荐答案

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

In your query you have part of the code like this:

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

ProductPrice是一个文本框,所以您需要使用属性 Text 访问文本:

ProductPrice is a textbox, so you need to use property Text to access the text:

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

另外,非常重要的注意事项:切勿像您一样通过连接命令来创建查询,因为您容易受到 SQL注入攻击的影响。有数千篇关于如何解决此问题的文章,请务必阅读并以适当的方式实施您的查询。

Also, very important note: NEVER create your query by concatenating command like you did, because you are vulnerable to SQL Injection attack. There are thousands of articles online about how to fix this issue, make sure to read them and implement your query in a proper way.

这篇关于将varchar值“System.Web.UI.WebControls.TextBox”转换为数据类型int时转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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