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

elmbrookAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rgn2121Commented:
before your rptView.ReportSource = cryRpt;  you need the line below...

cryRpt.SetDataSource(myds)
0
greatsubashCommented:
0
mlmccCommented:
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
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

elmbrookAuthor Commented:
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

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

mlmcc
0
elmbrookAuthor Commented:
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?
0
rgn2121Commented:
I do this exact thing, but how did you create your report?  For me, I had to write my schema out to an XML (xsd) file, then create my report off of that so that it would know my table structure.
Once that was done...I used the exact code shown below....sorry, it is in VB
 

'Load the report path
ReportDocument.Load(Path.Combine(filePath, reportName))
'Set the datasource to your DataSet
ReportDocument.SetDataSource(RptData)
'Set your formula if needed
CrystalReportViewer1.SelectionFormula = formula
'Set your report source...
CrystalReportViewer1.ReportSource = ReportDocument

Open in new window

0
rgn2121Commented:
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....
0
elmbrookAuthor Commented:
Hi All

Thank you for all your help. I have figured out what I was doing wrong. The datasource I was setting to the SqlDataAdapter not the DataSet...Duh

I have attached the code. I did not have to write it out to an XML Schema.

I now have a class which all reports will show in the one report browser.
            // Create new DataSet
            mydb thedb = new mydb();

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

            // Return Dataset
            DataSet myds = thedb.myDataSet(_sql, "SysUser");

            // Create Report
            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load(myglobal_properties.FP1 + "test.rpt");

            // Set Data Source to DataSet
            cryRpt.SetDataSource(myds);

            rptView.Visible = true;

            // Show Report
            rptView.ReportSource = cryRpt; 

public DataSet myDataSet(string sql, string tablename)
        {
            SqlDataAdapter thisAdapter = null;
            DataSet myds = new DataSet();

            if (!isConnected)
                return myds;

            SqlCommand thisCommand = new SqlCommand(sql, thisConnection);
            

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

                thisAdapter.Fill(myds, "mytablename");

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AgeovalCommented:
elmbrook, could you please tell me what does rptView mean in the code???
0
AgeovalCommented:
And the second question:
Is your DataSet is displayed in your Report after the final code?
0
elmbrookAuthor Commented:
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.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.