Hi PRA,
Refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
using Microsoft.Reporting.WinForms;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Imports Microsoft.Reporting.WinForms
Code
Form1.cs
private void Form1_Load(object sender, EventArgs e)
{
Data d = new Data();
DataTable dt = d.GetData();
dt.TableName = "Customers";
Customers dsCustomer = new Customers();
dsCustomer.Tables.Add(dt);
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomer.Tables["Customers"]);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(datasource);
this.reportViewer1.RefreshReport();
}
Data.cs
public DataTable GetData()
{
string constr = @"Data Source=.;Initial Catalog=Northwind;uid=.;pwd=.";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 * FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
Form1.vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim d As Data = New Data()
Dim dt As DataTable = d.GetData()
dt.TableName = "Customers"
Dim dsCustomer As Customers = New Customers()
dsCustomer.Tables.Add(dt)
Dim datasource As ReportDataSource = New ReportDataSource("Customers", dsCustomer.Tables("Customers"))
Me.ReportViewer1.LocalReport.DataSources.Clear()
Me.ReportViewer1.LocalReport.DataSources.Add(datasource)
Me.ReportViewer1.RefreshReport()
End Sub
Data.vb
Public Function GetData() As DataTable
Dim constr As String = "Data Source=.;Initial Catalog=Northwind;uid=.;pwd=."
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 * FROM Customers")
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Screenshot