Link to home
Start Free TrialLog in
Avatar of elmbrook
elmbrook

asked on

Creating a crystal report from a dataset

Hi

I have created a blank Crystal report called test.rpt.

I would like to manually add the information from a dataset to the report.

The report is loaded into Crystal Report Viewer.

The below code works in adding the report to the viewer but how would I connect my dataset to it?

I think I am close, just need help with the final display.

Thanks
private void rptBrowser_Load(object sender, EventArgs e)
        {
            // Create new DataSet
            mydb thedb = new mydb();
            SqlDataAdapter myds = null;

            string _sql = "select LogName, Firstname, Lastname from sysUser";

            myds = thedb.myDataAdapter(_sql);


            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load(myglobal_properties.FP1 + "test.rpt");
            rptView.ReportSource = cryRpt;
            rptView.Refresh();


          
        }

Open in new window

SOLUTION
Avatar of rgn2121
rgn2121
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike McCracken
Mike McCracken

Here is a VB.Net example for adding a dataset to a report

http://www.emoreau.com/Entries/Articles/2006/09/Feeding-Crystal-Reports-from-your-application.aspx

mlmcc
Avatar of elmbrook

ASKER

Greatsubash - I am not sure which article I am looking at
mimcc - I don't know VB and I am using VS2008. This article assumes you have a dataset created but I am trying to create one programically and link it to the Crystal Report.

Any help is always appreciated.

I tried adding the line cryRpt.SetDataSource(myds)
However, I am now getting a 'the data source object is invalid error message.

1. What am I doing wrong in my code
2. I have attached my code for creating a dataset below. Is there any way to see in the watch list what is contained in the dataset or a way to browse a dataset. There is no errors in creating the dataset but I would like to see if it contains any information.

 SqlDataAdapter thisAdapter = null;
            if (!isConnected)
                return thisAdapter;

            SqlCommand thisCommand = new SqlCommand (sql,thisConnection );
            myds = new DataSet();

            try
            {
                thisCommand.CommandText = sql;
                thisAdapter = new SqlDataAdapter(thisCommand );

                thisAdapter.Fill(myds, "DataTables"); 

                //Return result
                return thisAdapter;
            }
            catch (Exception ex)
            {
                string _error = ex.Message;
                return thisAdapter;
            }
            finally
            {
                thisCommand.Dispose();
                thisCommand = null;
            }

Open in new window

His creates the dataset in the program trhen passes it to the report.

mlmcc
Thanks Mimcc

I am running VS2008 so when I downloaded sample it was converted into VS2008. When I try to run the report I get a load error.

I am really looking for a solution for my code. Is there any way you are able to help me with that? or anyone else?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you check for data in your dataset?  Throw a datagridview on your form and set the datasource to your table in your dataset...make sure you have the data you expect....
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
elmbrook, could you please tell me what does rptView mean in the code???
And the second question:
Is your DataSet is displayed in your Report after the final code?
rptView is my form that displays the Crystal Report Viewer.

You have to connect the Dataset to the report first using the Database Expert, drop the fields in the report and then the information is displayed in the report.