繁体   English   中英

如何使用存储过程更新gridview中的数据?

[英]How to update data in gridview using a stored procedure?

我尝试使用存储过程更新网格视图中的数据,但我收到一个错误:

从对象类型System.Web.UI.WebControls.TextBox到已知的托管提供程序本机类型不存在映射。

存储过程:

CREATE PROCEDURE [dbo].[UpdateUser]
    @id int,
    @FirstName nvarchar(100)
AS
BEGIN
    UPDATE tblUsers 
    SET FirstName = @FirstName 
    WHERE id = @id 
END
GO

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
    TextBox FirstName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

    cmd = new SqlCommand("UpdateUser", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@id", id);
    cmd.Parameters.AddWithValue("@FirstName", FirstName);

    con.Open();
    cmd.ExecuteNonQuery();

    bindData();
    con.Close();
}

你能建议我怎么解决这个错误吗?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" DataKeyNames="id" ForeColor="#333333" GridLines="None"
     OnRowCancelingEdit="GridView1_RowCancelingEdit" 
    OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
     OnRowUpdating="GridView1_RowUpdating">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="id" runat="server" Text='<%# Eval("id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="First Name">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("FirstName") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblFname" runat="server" Text='<%# Eval ("FirstName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last Name">
            <ItemTemplate>
                <asp:Label ID="lblLname" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address">
            <ItemTemplate>
                <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("address") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email">
            <ItemTemplate>
                <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Phone">
            <ItemTemplate>
                <asp:Label ID="lblPhone" runat="server" Text='<%# Eval("Phone") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
    <EditRowStyle BackColor="#7C6F57" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F8FAFA" />
    <SortedAscendingHeaderStyle BackColor="#246B61" />
    <SortedDescendingCellStyle BackColor="#D4DFE1" />
    <SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>

您正在将TextBox变量传递给您的查询。 如果你这样做它应该工作:

cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);

问题是您正在将TextBox对象传递给AddWithValue方法。 由于它只识别本机类型,因此它会给出您看到的错误。

您应该传递Textbox.Text值,而不是对象本身。

将您的代码更改为:

cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);

编辑1:

尝试改变这个:

TextBox FirstName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

对此:

TextBox FirstName = (TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("TextBox1");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM