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

本文关键字:数据 gridview 更新 何使用 存储过程 | 更新日期: 2023-09-27 18:13:57

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

不存在从对象类型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>

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

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

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

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

你应该传递文本框。文本值,而不是对象本身。

把你的代码改成这样:

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");