Hi,

in my project I need to allow the user (admin user) to have the ability to run custom queries on the database. Ive done this pretty simply whwer the user enters their query and the reults are set to a datagridview.

It would also be great however to allow the user to print the results of their custom query. For this I thought the easiest thing to do would be to use a crystal report.

However the problem Im encountering is that because the dataset that the datagrid is populated from is empty until the user fires the query, the crystal report cant find any source for the data to be displayed.

Can anyone think of a better way to do this or am I approaching this in completely the wrong way?

Hope that makes sense and thanks in advance!

Dani AI

Generated

— the real blocker here is not a bug in Crystal but a schema problem. Crystal Reports needs a design-time schema (fields it knows about); if you design a report against an empty, ad-hoc DataSet the runtime report has no fields to bind. 's suggestion to avoid trying to print when there are no rows is solid as a UX check, but you also need a stable schema and a clear runtime binding step.

A reliable approach:

  • Create a typed DataSet (.xsd) in your project that contains a DataTable with the columns your reports will use (you can make it a superset of likely columns).
  • Design the .rpt against that typed DataSet so Crystal has field definitions.
  • At runtime fill a DataTable with the admin's query and bind that table to the report.

Example VB.NET workflow:

' fill dt from the user SQL (validate and restrict SQL first)
Dim dt As New DataTable()
Using da As New SqlDataAdapter(userSql, conn)
    da.Fill(dt)
End Using

' bind to the report designed against a compatible XSD
Dim rpt As New ReportDocument()
rpt.Load("CustomReport.rpt")
rpt.SetDataSource(dt)
CrystalReportViewer1.ReportSource = rpt
CrystalReportViewer1.Refresh()

If you truly must support arbitrary column sets, Crystal is awkward because fields must be known at design time. Options in that case: render/print the DataGridView with PrintDocument, export to HTML/CSV/PDF for printing, or use a reporting engine that supports runtime schemas (or transform results into a generic name/value table).

Important notes: restrict and validate the admin SQL (allow only SELECT, impose timeouts/row limits), ensure DataTable column names/types match the XSD, and handle empty-result cases gracefully instead of loading the report.

hi...

suppose your dataset is DS

check this kind of condition..

if DS.Tables(0).Rows.Count >0 then

"load your report"

else

MessageBox.Show("Nothing to Print")

End IF
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.