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

时间:2016-10-20 15:45:40

标签: c# asp.net

我尝试使用存储过程更新网格视图中的数据但是出现错误:

  

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

问题是您正在将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");