在Gridview中启动存储过程

时间:2015-11-29 22:19:27

标签: asp.net sql-server stored-procedures gridview

我在向GridView添加列时遇到问题,这会触发存储过程的启动。

我正在尝试创建一个库应用程序。

我在GridView中显示可用的书籍,我需要添加一个新列,它将保存存储过程的触发器并获取列id_book,作为存储过程的参数。

这是我的笔记输出。我需要什么。谢谢你的帮助。

  • 添加新列Lend book。
  • 在每个列中都有一个链接,该链接将触发存储过程并从当前行获取id_book

enter image description here

我的gridview标记的一部分:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id_book" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="id_book" HeaderText="id_book" InsertVisible="False" ReadOnly="True" SortExpression="id_book" />
        <asp:BoundField DataField="book_name" HeaderText="book_name" SortExpression="book_name" />
        <asp:BoundField DataField="id_author" HeaderText="id_author" SortExpression="id_author" />
        <asp:CheckBoxField DataField="state" HeaderText="state" SortExpression="state" />
    </Columns>
</asp:GridView>

我的存储过程:

CREATE PROCEDURE [dbo].[lendBook_sp]
    @id_reader int,
    @id_book int
AS
BEGIN
    INSERT INTO dbo.lendBook (id_book, id_reader, lendDate)  
    VALUES (@id_book, @id_reader, GETDATE())

    UPDATE dbo.book
    SET state = 0
    WHERE id_book = @id_book
END

1 个答案:

答案 0 :(得分:1)

在这里,我编辑了您的HTML代码,以便将列添加到网格

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id_book" DataSourceID="SqlDataSource1" OnRowCommand="gridView_RowCommand" >
<Columns>
    <asp:BoundField DataField="id_book" HeaderText="id_book" InsertVisible="False" ReadOnly="True" SortExpression="id_book" />
    <asp:BoundField DataField="book_name" HeaderText="book_name" SortExpression="book_name" />
    <asp:BoundField DataField="id_author" HeaderText="id_author" SortExpression="id_author" />
    <asp:CheckBoxField DataField="state" HeaderText="state" SortExpression="state" />

    <asp:ButtonField CommandName="EditRow" Text="Lend" CausesValidation=false></asp:ButtonField>
</Columns>

以下是当用户点击&#34; Lend&#34;时将调用的事件的C#代码。链接。

 protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("EditRow"))
    {
        int _bookIDEdit = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument.ToString())].Values["id_book"].ToString());

    }
}

将BookID获取到intege变量后,您可以调用StoredProcedure。

相关问题