[Solved] ASP.Net Crystal Reports is not populating Report with data

mahjoubi
 
on Aug 25, 2022 11:05 PM
464 Views

this is my try and nothing appear and i used the same dataset in the example

please help what i missed to get the data populated in CrystalReportViewer?

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Public Class PrintingUsinCR
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim crystalReport As New ReportDocument()
        crystalReport.Load(Server.MapPath("~/CrystalReport1.rpt"))
        Dim dsCustomers As Customers = GetData("SELECT [DP_No],[DP_Code],[DP_Intitule],[DP_Zone] FROM [STUPREMAT].[dbo].[F_DEPOTEMPL]
  where DP_Intitule = 'A0013' order by DP_Intitule")
        crystalReport.SetDataSource(dsCustomers)
        CrystalReportViewer1.ReportSource = crystalReport
    End Sub
    Private Function GetData(query As String) As Customers
        Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim cmd As New SqlCommand(query)
        Using con As New SqlConnection(conString)
            Using sda As New SqlDataAdapter()
                cmd.Connection = con

                sda.SelectCommand = cmd
                Using dsCustomers As New Customers()
                    sda.Fill(dsCustomers, "DataTable1")
                    Return dsCustomers
                End Using
            End Using
        End Using
    End Function
End Class

 

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPage.Master" CodeBehind="PrintingUsinCR.aspx.vb" Inherits="WebApplication11.PrintingUsinCR" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
      <script src="Crystal%20Reports%20for%20.NET%20Framework%204.0/Common/Crystal%20Reports%202011/crystalreportviewers/js/crviewer/crv.js"></script>

    <style type="text/css">
        .auto-style4 {
            width: 100%;
            height: 58px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlacebody" runat="server">
    <table class="auto-style4">
        <tr>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <asp:GridView ID="GridView1" runat="server" Width="715px"></asp:GridView>
            </td>
        </tr>
    </table>
    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
    </asp:Content>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
PrinceG
 
on Aug 26, 2022 01:12 AM
on Aug 26, 2022 01:14 AM

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