Reputation: 53
I tried to use the code below, but it doesn't work.
I stored report data in a ReportDataSet
using an SQL query. Later I set this ReportDataSet
as the DataSource
for the CrystalReport
object.
private void btnGenReport_Click(object sender, EventArgs e)
{
CrystalReport1 objRpt = new CrystalReport1();
string sql; //creating sql query
sql = "SELECT invoice.InvoiceNo, invoice.Date, invoiceitems.Name, invoice.InvoiceTo, invoice.CusName, invoiceitems.Qty, invoiceitems.Rate, invoiceitems.Amount ";
sql += "FROM (invoice INNER JOIN invoiceitems ON invoice.Date = invoiceitems.Date) ";
sql += "WHERE (invoice.Date BETWEEN '" + DateTimePickerFrom.Value + "' AND '" + DateTimePickerTo.Value + "');";
ReportDataSet ds = new ReportDataSet();
string path = Application.StartupPath + "\\";
string conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + path + "SMS_DB.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(conStr); // create connection
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds, "InvoiceItemData");
objRpt.SetDataSource(ds.Tables["InvoiceItemData"]);
crystalReportViewer1.ReportSource = objRpt;
con.close();
}
catch (Exception ex)
{
MessageBox.Show("Error ! \n" + ex.ToString());
}
}
Upvotes: 1
Views: 4570
Reputation: 1922
I'm going to assume a few things:
ReportDataSet
is setup using VS's Dataset Designer via a connection to your database file SMS_DB.mdf and that there is data already in this filert1
is setup within the Report Designer with the datasource being the table InvoiceItemData
from the typed dataset ReportDataSet
in your projectIn other words, I'm assuming the conditions in which you've set everything up already, you've done everything up to the btnReportGen
button click within the latest, non-beta environment, and that you're not trying to run this as a Release on another machine.
Now, I have some suggestions and clarifications to make to you.
If you consider all of the above, I think you'll find yourself with a correctly working project. However, if you are still stuck, I can go through each suggestion and hand-hold you through fixing everything with screenshots and example code. It's okay if you need me to do that because I've created a huge variety of Crystal Report projects in VS2010 and I'm just telling you, from experience, what works best for you as the programmer and for the user. You might think you're saving time and resources by just typing everything up programmatically and throwing it at the report viewer, but that's also the reason why it's not cooperating.
Upvotes: 1