简体   繁体   English

在Gridview中启动存储过程

[英]Start stored procedure in Gridview

I have a problem with adding column into GridView , which will trigger a start of a stored procedure. 我在GridView添加列有问题,这将触发存储过程的启动。

I'm trying to make a library application. 我正在尝试制作图书馆应用程序。

I am displaying available books in a GridView and I need to add a new column from which it will hold the trigger of a stored procedure and get column id_book , as parameter for the stored procedure. 我在GridView显示可用的书,我需要添加一个新列,该列将从中保存存储过程的触发器,并获取id_book列作为存储过程的参数。

This is the output with my notes. 这是我的笔记的输出。 What do I need. 我需要什么。 Thank you for help. 谢谢你的帮助。

  • Add a new column Lend book. 添加新列借出书。
  • In every column have a link that will trigger stored procedure and get id_book from current row. 在每一列中都有一个链接,该链接将触发存储过程并从当前行获取id_book

在此处输入图片说明

Part of my gridview markup: 我的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>

My stored procedure: 我的存储过程:

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

Here I have edited your HTML code to add column to the Grid 在这里,我编辑了您的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>

Below is the C# code for the event which will be called when the user clicks on the "Lend" link. 下面是事件的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());

    }
}

After getting the BookID to the intege variable, you can call the StoredProcedure. 将BookID获取到intege变量后,可以调用StoredProcedure。

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

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