Hi mahjoubi,
You need to pass the Table name for selecting the Table from DataSet and set in DataSource. In your case its DataTable1.
Please refer below example.
MasterPage
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
HTML
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table class="auto-style4">
<tr>
<td>
<asp:Button ID="btnGetDetails" runat="server" Text="GetDetails" />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False">
</asp:GridView>
</td>
</tr>
</table>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
</asp:Content>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
this.BindCrystalReport();
}
private void BindCrystalReport()
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/Customers.rpt"));
DataSet1 dsCustomers = GetData("SELECT TOP 20 CustomerId,ContactName,City,Country FROM Customers");
crystalReport.SetDataSource(dsCustomers.Tables["DataTable1"]);
CrystalReportViewer1.ReportSource = crystalReport;
}
private DataSet1 GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.Connection = con;
using (DataSet1 dsCustomers = new DataSet1())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.BindCrystalReport()
End Sub
Private Sub BindCrystalReport()
Dim crystalReport As ReportDocument = New ReportDocument()
crystalReport.Load(Server.MapPath("~/Customers.rpt"))
Dim dsCustomers As DataSet1 = GetData("SELECT TOP 20 CustomerId,ContactName,City,Country FROM Customers")
crystalReport.SetDataSource(dsCustomers.Tables("DataTable1"))
CrystalReportViewer1.ReportSource = crystalReport
End Sub
Private Function GetData(ByVal query As String) As DataSet1
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
cmd.Connection = con
Using dsCustomers As DataSet1 = New DataSet1()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Using
End Function
Screenshot