简体   繁体   English

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

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

I am try to update data in grid view using a stored procedure but I get an error: 我尝试使用存储过程更新网格视图中的数据,但我收到一个错误:

No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type. 从对象类型System.Web.UI.WebControls.TextBox到已知的托管提供程序本机类型不存在映射。

Stored procedure: 存储过程:

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

Code

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

Can you suggest me how to solve this error? 你能建议我怎么解决这个错误吗?

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

You are passing a TextBox variable to your query. 您正在将TextBox变量传递给您的查询。 If you do this it should work: 如果你这样做它应该工作:

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

The problem is that you are passing a TextBox object to the AddWithValue method. 问题是您正在将TextBox对象传递给AddWithValue方法。 Since it only recognizes native types it gives the error you are seeing. 由于它只识别本机类型,因此它会给出您看到的错误。

You should pass the Textbox.Text value, not the object itself. 您应该传递Textbox.Text值,而不是对象本身。

Change your code to be like this: 将您的代码更改为:

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

EDIT 1: 编辑1:

Try changing this: 尝试改变这个:

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

To this: 对此:

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

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

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