Hi ChandraDCS,
Check this sample. now take its reference.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="ID" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnView" Text="View Order" runat="server" OnClick="OnViewOrder" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<hr />
<rsweb:ReportViewer ID="ReportViewer1" runat="server">
</rsweb:ReportViewer>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindCustomers();
}
}
protected void OnViewOrder(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)(sender as Button).NamingContainer);
string customerID = row.Cells[0].Text;
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
NorthwindOrders dsOrders = GetOrder(customerID);
ReportDataSource datasource = new ReportDataSource("DataSet1", dsOrders.Tables["Orders"]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
private NorthwindOrders GetOrder(string CustomerID)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT OrderID,CustomerID,Freight FROM Orders WHERE CustomerID=@CustomerID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@CustomerID", CustomerID);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (NorthwindOrders dsOrder = new NorthwindOrders())
{
sda.Fill(dsOrder, "Orders");
return dsOrder;
}
}
}
}
}
private void BindCustomers()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 CustomerID,ContactName,City,Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindCustomers()
End If
End Sub
Protected Sub OnViewOrder(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = (CType((TryCast(sender, Button)).NamingContainer, GridViewRow))
Dim customerID As String = row.Cells(0).Text
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim dsOrders As NorthwindOrders = GetOrder(customerID)
Dim datasource As ReportDataSource = New ReportDataSource("DataSet1", dsOrders.Tables("Orders"))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End Sub
Private Function GetOrder(ByVal CustomerID As String) As NorthwindOrders
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT OrderID,CustomerID,Freight FROM Orders WHERE CustomerID=@CustomerID", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@CustomerID", CustomerID)
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Using dsOrder As NorthwindOrders = New NorthwindOrders()
sda.Fill(dsOrder, "Orders")
Return dsOrder
End Using
End Using
End Using
End Using
End Function
Private Sub BindCustomers()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 10 CustomerID,ContactName,City,Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot
