简体   繁体   中英

crystal report got error when copy to datatable

i got problem about reporting using crystal report

here is my code

protected void Page_Load(object sender, EventArgs e)
    {
        string Judul, Peminjam, Pinjam, Kembali;
        DataTable dtReturn = new DataTable();
        SqlConnection con = data.getKoneksi();
        con.Open();
        SqlCommand Perintah = new SqlCommand();
        Perintah.Connection = con;
        Perintah.CommandText = "SELECT DISTINCT Peminjaman.Katalog, Peminjaman.Pinjam, Peminjaman.Kembali, Pengguna.Nama, Buku.Judul, Buku.Jumlah, Temp.Kembali AS HrsKbl FROM Peminjaman INNER JOIN Buku INNER JOIN Temp ON Buku.ID = Temp.ID ON Peminjaman.Katalog = Buku.ID AND Peminjaman.Katalog = Temp.ID INNER JOIN Pengguna ON Peminjaman.Peminjam = Pengguna.NIS WHERE Peminjaman.Status = 'dipinjam' ORDER BY Kembali ASC";
        SqlDataAdapter Adapter = new SqlDataAdapter();
        Adapter.SelectCommand = Perintah;
        Adapter.Fill(dtReturn);
        LapBulanan.LaporanBulananDataTable dt = new LapBulanan.LaporanBulananDataTable();
        foreach (DataRow dr in dtReturn.Rows)
        {
            Judul = dr["Judul"].ToString();
            Peminjam = dr["Nama"].ToString();
            Pinjam = dr["Pinjam"].ToString();
            Kembali = dr["Kembali"].ToString();
        }

        DataTable dtTemp = dt;
        ReportDocument rpt = new Laporan();
        rpt.Load(Server.MapPath("~/Laporan.rpt"));
        rpt.SetDataSource(dtTemp);
        rpt.SetDataSource(dt.CopyToDataTable());
        CrystalReportViewer1.ReportSource = rpt;
        CrystalReportViewer1.DataBind();
        rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "Laporan");
        con.Close();
    }

my dataset name is LapBulanan.xsd and contain datatable name LaporanBulanan(Judul, Peminjam, Pinjam, Kembali)

when i debug this program i got an error message "The source contains no DataRows" but when i check this query i got the correct record

when i remove "rpt.SetDataSource(dt.CopyToDataTable());" this crystal report is running well but i got no record on my report

thanks for your kindness

First of all, the dtReturn datatable which has the result of the SQL query is never being used. Instead the dtTemp datatable that you are using contains no data because you just assign to it an empty instance of the dt datatable.

The foreach loop does nothing at all because it just assigns values to 4 variables that change in every iteration and never being used after that.

You don't need to set the datasource of the report twice.

So I think the code should look something like this

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtReturn = new DataTable();
        SqlConnection con = data.getKoneksi();
        con.Open();
        SqlCommand Perintah = new SqlCommand();
        Perintah.Connection = con;
        Perintah.CommandText = "SELECT DISTINCT Peminjaman.Katalog, Peminjaman.Pinjam, Peminjaman.Kembali, Pengguna.Nama, Buku.Judul, Buku.Jumlah, Temp.Kembali AS HrsKbl FROM Peminjaman INNER JOIN Buku INNER JOIN Temp ON Buku.ID = Temp.ID ON Peminjaman.Katalog = Buku.ID AND Peminjaman.Katalog = Temp.ID INNER JOIN Pengguna ON Peminjaman.Peminjam = Pengguna.NIS WHERE Peminjaman.Status = 'dipinjam' ORDER BY Kembali ASC";
        SqlDataAdapter Adapter = new SqlDataAdapter();
        Adapter.SelectCommand = Perintah;
        Adapter.Fill(dtReturn);

        ReportDocument rpt = new Laporan();
        rpt.Load(Server.MapPath("~/Laporan.rpt"));
        rpt.SetDataSource(dtReturn);

        CrystalReportViewer1.ReportSource = rpt;
        CrystalReportViewer1.DataBind();
        rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "Laporan");
        con.Close();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM