Start stored procedure in Gridview

I have a problem with adding column into GridView, which will trigger a start of a stored procedure.

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.

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.

Part of my gridview markup:

<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

Answers


Here I have edited your HTML code to add column to the Grid

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

 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.


Need Your Help

How ARM and DSP processor work together?

multiprocessing processor microprocessors ti-dsp

How do an ARM and DSP processor work together , there instruction set being different , how can one write a code which is supposed to work on DSP processor such as FFT , Video Processing etc. How d...

Checking if an expression is balanced in terms of parantheses

java nested stack

I'm trying to check if an expression is balanced in terms of its parentheses , my program should output the appropriate messages as follows: (I'm reading the expression from a file)