I have tried numerous times to resolve the error I am getting with this and put up my watch window and it says my reader object drReport is throwing an exception but the application runs and displays the report viewer with the message "The source of the report definition has not been specified. I am forwarding my code...any help appreciated.
private void Form1_Load(object sender, EventArgs e)
{
//use following if you use standard security
string cnString = @"Data Source= sparrishlpt2\stacy;Initial Catalog=northwind; Integrated Security=SSPI";
//declare Connection, command and other related objects
SqlConnection conReport = new SqlConnection(cnString);
SqlCommand cmdReport = new SqlCommand();
SqlDataReader drReport;
DataSet dsReport = new dsProduct();
try
{
//open connection
conReport.Open();
//prepare connection object to get the data through reader and populate into dataset
cmdReport.CommandType =
CommandType.Text;
cmdReport.Connection = conReport;
cmdReport.CommandText =
"Select TOP 5 * FROM Products Order By ProductName";
//read data from command object
drReport = cmdReport.ExecuteReader();
//new cool thing with ADO.NET... load data directly from reader to dataset
dsReport.Tables[0].Load(drReport);
//close reader and connection
drReport.Close();
conReport.Close();
//provide local report information to viewer
reportViewer1.LocalReport.ReportEmbeddedResource =
"rsWin1.rptProductList.rdlc";
//prepare report data source
ReportDataSource rds = new ReportDataSource();
rds.Name =
"dsProduct_dtProductList";
rds.Value = dsReport.Tables[0];
reportViewer1.LocalReport.DataSources.Add(rds);
//load report viewer
reportViewer1.RefreshReport();
}
catch (Exception ex)
{
//display generic error message back to user
MessageBox.Show(ex.Message);
}
finally
{
//check if connection is still open then attempt to close it
if (conReport.State == ConnectionState.Open)
{
conReport.Close();
}
}