簡體   English   中英

將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(“”)起作用時,它會返回0作為值。 但其他數據運行正常。 當我插入findcontrol(txtboxProductPrice)時,它向我顯示錯誤:

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

我的桌子包含

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攻擊的攻擊 在線上有成千上萬篇有關如何解決此問題,確保閱讀它們並以適當方式實施查詢的文章。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM