Bind multiple ASP.Net GridViews using single Stored Procedure in C# and VB.Net

micah
 
on Mar 26, 2021 07:23 AM
928 Views

I want to display Gridview record from two tables on two GridView, the Payment record will be displayed on GridView one while Item Order table will be displayed on GridView2, so ones the user inserts receipt number and the date the transaction took place and clicks search button the two records will be displayed on the two GridViews.

ID,Date,Receipt,Name,Phone,Address,Payment_Status,Payment_Method,Bank_Name,Account_Number,Total,AmountPaid,Balance.

Item Order table below ID, Date, Reciept,Brand,Quantity,Price 

ALTER PROCEDURE [dbo].[GetReportsDate]
    @From DateTime,
    @ToDate DateTime     
AS
    SELECT ID,Date,Receipt,Name,Phone,Address,Payment_Status,Payment_Method,Bank_Name,Account_Number,Total,AmountPaid,Balance
    FROM Payment WHERE (Date BETWEEN @From AND @ToDate) 
    RETURN 0
END

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 26, 2021 07:33 AM
on Jan 05, 2022 11:25 AM

Use two select query in the procedure to select record from multiple table.

Call the procedure and fill the record in DataSet.

Then use the DataSet tables properties and assign to GridView.

Check the sample code.

C#

private void BindGridView()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        SqlCommand cmd = new SqlCommand("GetReportsDate");
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);

                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                GridView2.DataSource = ds.Tables[1];
                GridView2.DataBind();
            }
        }
    }
}

VB.Net

Private Sub BindGridView()
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Dim cmd As SqlCommand = New SqlCommand("GetReportsDate")
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using ds As DataSet = New DataSet()
                sda.Fill(ds)
                GridView1.DataSource = ds.Tables(0)
                GridView1.DataBind()
                GridView2.DataSource = ds.Tables(1)
                GridView2.DataBind()
            End Using
        End Using
    End Using
End Sub