Crystal Reports is not showing records from the da

2020-05-06 07:19发布

问题:

I am trying to display the records from one of the tables created in a database. When I run the application, it loads but I can only see the columns without the records ( records exists). In my design mode I have placed CrystalReportViewer having the source of the CrystalReport created.

I am using VS 2015 with CrystalReports v 13.0.8

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm.aspx.cs" Inherits="WebForm" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="/crystalreportviewers13/js/crviewer/crv.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" GroupTreeImagesFolderUrl="" Height="1202px" ReportSourceID="CrystalReportSource1" ToolbarImagesFolderUrl="" ToolPanelView="None" ToolPanelWidth="200px" Width="1104px" />
        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
            <Report FileName="CrystalReport2.rpt">
            </Report>
        </CR:CrystalReportSource>
    </div>
    </form>
</body>
</html>

In my .cs I have the code above. My table is called Salarii

using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

public partial class WebForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("~/CrystalReport.rpt"));
        DataSet dsSalarii = GetData("select * from Salarii");
        crystalReport.SetDataSource(dsSalarii);
        CrystalReportViewer1.ReportSource = crystalReport;
    }

     private DataSet GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionString;              
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataSet dsSalarii = new DataSet())
                {
                    sda.Fill(dsSalarii, "DataTable1");
                    return dsSalarii;
                }
            }
        }
    }


}

I am very new to this and followed tutorials during the developing. I need help, this is a very important project at school and would appreciate every help I can get.

回答1:

  1. Try to debug, step by step.. does your dsSalarii has some data? if yes, go next step
  2. You must to ensure, that name of DataTable (in your case : "DataTable1") is the same in CrystalReport, if yes, go next step
  3. It s not enough to only Set DataSource to a report, you must pass data to a report :

    ds.WriteXml(XmlDataPath)
    

save your dataset as xml, now you can add your generated xml to a Crystal Report, so open Crystal Report, go to Database> Database expert> Create New Connection> ADO.NET(xml) and choose path to generated xml

next step is putting database fields in report