Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I've got a problem which I can't solve by myself.

I have a Windows Form (RepF) which contains a Microsoft Report Viewer (Report1.rdlc) component.

Also, I have another Windows Form (Principal) where I fill in the information about the invoice, the system automatically generates the number of this invoice and insert it into the database (MySQL).

When I click over the button that stores these information into the database, the system shows me a window asking me if I want to print it. If I click YES, the system opens the Windows Form RepF, sending the invoice number as a parameter to this Windows Form constructor.

C#
public RepF(string invoiceNumber)
{
    InitializeComponent();
    _invoiceNumber = invoiceNumber;
}


After this, in the RepF_Load I initialize some DataTables and fill them with MySqlDataAdapters.

      private void RepF_Load(object sender, EventArgs e)
        {
            this.reportViewer1.RefreshReport();

            StreamReader stR = File.OpenText(filePath);

            while (sLine != null)
            {
                sLine = stR.ReadLine();
                if (sLine != null)
                { fileEntries.Add(sLine); }
            }

            _hostName = fileEntries[0].ToString();
            _database = fileEntries[1].ToString();
            _portNumber = fileEntries[2].ToString();
            _userName = fileEntries[3].ToString();
            _userPassword = fileEntries[4].ToString();

            bdConn = new MySqlConnection("Server=" + _hostName + ";Port=" + _portNumber + ";Database=" + _database + ";Uid=" + _userName + ";Pwd=" + _userPassword + "; Connection Timeout = 3");
            string teste = "Server=" + _hostName + ";Port=" + _portNumber + ";Database=" + _database + ";Uid=" + _userName + ";Pwd=" + _userPassword;
            bdConn.Open();


            MySqlCommand mscInvoiceDetails = new MySqlCommand("SELECT invoice_code, dt_invoice, total_value, observation FROM tb_invoices WHERE invoice_code = \'" + _invoiceNumber + "\';", bdConn);
            MySqlCommand mscInvoiceDescriptionDetails = new MySqlCommand("SELECT detail_description, detail_value, ind_taxible FROM tb_doc_details WHERE tb_invoices_id_invoice = (SELECT id_invoice FROM tb_invoices WHERE invoice_code = \'" + _invoiceNumber + "\');", bdConn);
            MySqlCommand mscClientDetails = new MySqlCommand("SELECT company_name, company_registration, address, corporate_name FROM tb_client WHERE id = (SELECT tb_client_id FROM tb_invoices WHERE invoice_code = \'" + _invoiceNumber + "\');", bdConn);
            MySqlCommand mscPaymentTypeDetails = new MySqlCommand("SELECT payment_type_name, payment_type_description FROM tb_payment_type WHERE id = (SELECT tb_payment_type_id FROM tb_invoices WHERE invoice_code = \'" + _invoiceNumber + "\');", bdConn);
            MySqlCommand mscDocumentTypeDetails = new MySqlCommand("SELECT doc_type_cd FROM tb_doc_type WHERE id_doc_type = (SELECT tb_doc_type_id_doc_type FROM tb_invoices WHERE invoice_code = \'" + _invoiceNumber + "\');", bdConn);


            MySqlDataAdapter mscDAdtInvoiceDetails = new MySqlDataAdapter(mscInvoiceDetails);
            mscDAdtInvoiceDetails.Fill(dtInvoiceDetails);

            MySqlDataAdapter mscDAdtInvoiceDescriptionDetails = new MySqlDataAdapter(mscInvoiceDescriptionDetails);
            mscDAdtInvoiceDescriptionDetails.Fill(dtInvoiceDescriptionDetails);

            MySqlDataAdapter mscDAdtClientDetails = new MySqlDataAdapter(mscClientDetails);
            mscDAdtClientDetails.Fill(dtClientDetails);

            MySqlDataAdapter mscDAdtPaymentTypeDetails = new MySqlDataAdapter(mscPaymentTypeDetails);
            mscDAdtPaymentTypeDetails.Fill(dtPaymentTypeDetails);

            MySqlDataAdapter mscDAdtDocumentTypeDetails = new MySqlDataAdapter(mscDocumentTypeDetails);
            mscDAdtDocumentTypeDetails.Fill(dtDocumentTypeDetails);
}

private void reportViewer1_Load(object sender, EventArgs e)
{

}



As you can see, I don't have a DataSet, as I am creating and populating my DataTables during the code execution, and, the Microsoft Report Viewer component is in this Windows Form (RepF) class.

This Windows Report Viewer has its report already configured (with text boxes and a table).

What I need to know is:

How can I use the data stored in those DataTables in my report (Report1.rldc), to see and print those information?


Notes:

1 - I am using Visual Studio Team System - C#
2 - I am not an experienced programmer


Thanks in advance! :)
Posted
Updated 18-Nov-10 4:00am
v7

Create a new dummy dataset in your solution & design a table with the columns you want (empty of course). Now use the fields of this table in your report designer (drag & drop will do).

In your code, fill up a DataTable with the same structure as in your dataset.
Add the DataTable to the report data sources.
Eg

ReportViewer1.LocalReport.DataSources.Add( _
New Microsoft.Reporting.WinForms.ReportDataSource("ds1_tbl", dt))

where "ds1" is the name of the dataset you have designed and "tbl" is the name of the table in the dataset you designed
(don't forget the underscore in between.)
and
dt is the DataTable that you have filled
Thats it. Refresh the report & you should get it.
 
Share this answer
 
Comments
lucasgrohl 16-Nov-10 19:53pm    
Strogg,

I created a DataSet, named DataSet1, and added a DataTable in it, named DataTable1, with the fields "field1", "field2" and "field3".

I added the DataSet1 as a DataSource to my Report1.rdlc through the visual mode and dragged and dropped the DataTable1 fields into my table contained in my Report1.rdlc

Now, in the RepF_Load my code is filling in my DataTables as shown in my question and at the end I put the code:

private void RepF_Load(object sender, EventArgs e)
{ ...

reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_DataTable1", dtInvoiceDescriptionDetails));

reportViewer1.RefreshReport();
}

But, when I execute the program it is still empty.

Do you have any idea of the reason this is not working yet?

Thanks! =)
Make sure of these things
1) The values of the fields in the table (in report designer) are set like
=Fields!field1.Value , =Fields!field2.Value etc
2) That you have created a DataTable containing the same field names (field1,field2,field3) and they have the same data types.
3) Make sure that the DataTable has some rows before adding it using DataSources.Add()
4) In the report designer, select the table, right click & choose properties. In the General tab, check the 'Dataset Name' - it should be "DataSet1_DataTable1". Make sure you are using the same name while adding datasources. (case sensitive)
5) In the report viewer control, make sure you have picked your report.rdlc file. If you are keeping the rdlc file as an external resource, tell the control that : ReportViewer1.LocalReport.ReportPath = "C:\\xxx.rdlc";

If you're still getting an error let me know the error the reportviewer control is showing.
 
Share this answer
 
Comments
lucasgrohl 18-Nov-10 2:09am    
I check all the items.

I changed my DataTable1's fields name in my DataSet1 to the same name as it returns in my DataTable dtInvoiceDescriptionDetails

The values in the table in my Rdlc are:
=Fields!detail_description.Value / =Fields!detail_value.Value / =Fields!ind_taxible.Value
(the same name as in my datasource datatable)

I checked the data straight after it has been filled in on the code. It is full populated.

I changed the Dataset Name to "DataSet1_DataTable1".

And the Rdlc is in the project, so I don't need the add its path.

When I execute the programme there is no error. Only the data doesn't appear.
The table in my Rdlc shows the hearder with the name of the fields, but no data.

Any idea? =(

thanks!
All you had to do was to delete those designer generated bindings : )
I don't know about your problem. weird.
I had some funny problems when i maintained 2 tables in one dataset. After trying lots of things, it worked correctly when i moved each table to a unique dataset. Try that - just keep only one table in one DataSet in the designer
Eg:
DataSet1_DataTable1,
DataSet2_DataTable2 etc.
drag & drop fields from the related Datasets & add Report DataSources for all of them & refresh report

It might work
 
Share this answer
 
Sorry to be posting in here, but the comment area is having problems and I can't do it there. When it comes ok I move this post to the right place.

Strogg,

I created another dataset and used it but the same problem happens.
for example... a textbox receiving the invoice number is working:

="Invoice N°: " + First(Fields!invoice_code.Value, "DataSet2_DataTable1")

But, a textbox receiving the date is not working. It only appears the word "Date: "

="Date: " + First(Fields!invoice_date.Value, "DataSet2_DataTable1")

I checked and saw that the datatable is retrieving this value... but... doesn't show =(

The field date is also stored as a string (2010/19/11).

any idea? =(
 
Share this answer
 
Comments
strogg 19-Nov-10 12:25pm    
I dont understand. Should work especially if 'date' is String. All i can say is check for case & try in a new dataset / report maybe:
I wont be able to solve unless i see it :(
lucasgrohl 19-Nov-10 16:06pm    
Well, I used a new DataSet and as I it's with the right case, otherwise I think the DataTasble that is being used as a DataSource wouldn't even be populated. It has to be in the same report because this report needs to encompass 2 DataTables (each one in different DataSets).

And in this case I can't even send you the project for you to see because you wouldn't have the data base... =/

Are there any way to debug in run time what is being passed to the form and loaded in it? I mean, check all the steps and verify in run time what is in each field?

Thanks!
strogg 21-Nov-10 8:59am    
unfortunately you can only debug till the table fills up & send it to the report. But try some other tricks. Maybe your First() record may be blank? Try Last(Fields!...Value). You can also check the number of records actually received by the report with =CountRows(Nothing) or =Count(Fields!xxx.Value, Nothing). Better yet, add a table & drop a few fields from the troublesome tables to check.
& I'm running out of ideas ;-)
lucasgrohl 21-Nov-10 10:12am    
Strogg, what I did was to use cstr() in each field. Now everything is working. I have no idea of why it wasn't but now it is ok.

Finally, I finished my application! =) Tomorrow will be the presentation day.

Because of you I have finished it in time!

Thanks a lot man! =)
strogg 21-Nov-10 11:26am    
Whew! Good luck
Hello friends!!!

i just have created a windows application and i have also used Reportviewer in my project...the problem i'm facing that whenever i make an entry in my application and try to see the report of it, ican't see it...

But after closing the application and restarting it, i can see the report of last inserted record....but can't see immedieatly....

i'm using c#.net 2005 and sqlexpress.....also .rdlc file, dataset1(inbuilt) means not to through coding..just set parameters statically...

Any idea friends???
Please tell me if you know the solution...
Thanks in advance....
 
Share this answer
 
Hey, you can't change the name of the dataset in the rdlc's table. U must simply use the same name in your code. Changing the name there won't change the dataset's name u had created in the designer.
Also, in the designer on the control, when you choose the rdlc, the designer automatically adds a binding source & data source in the form. Those might be playing games with you. Delete them and run. If you can't find them, you can also clear the sources in the code before you assign the datasrouce
: LocalReport.DataSources.Clear()
LocalReport.DataSources.Add(....
You must get it now, or you've made some mistake.
== Edit ==
Removed a dead link
 
Share this answer
 
v2
Comments
lucasgrohl 18-Nov-10 7:16am    
Man, thanks a LOT!!! Now it works!!! It is alive!!! =D
What I did was to update the databinds on the Report Viewer control and it worked perfectly!
Now is just do it for the rest of the stuff and my system will be complete!

Again, thanks a lot for your time and patience to help me!
lucasgrohl 18-Nov-10 8:54am    
I am sorry mate, just one simple question... I must be doing something wrong...
I created another table with all the other fields I need and added them to some text boxes in my Rdlc. Some of them appear correctly, but some doesn't appear.
Also, one of the text boxes is receiving 3 of the fields... 2 appear ok, but the last one doesn't appear.

They are like this in one textbox:

="TO: " + First(Fields!corporate_name.Value, "DataSet1_DataTable2")
+ First(Fields!company_registration.Value, "DataSet1_DataTable2")
+ First(Fields!company_address.Value, "DataSet1_DataTable2")

The company address doesn't appear.

Do you have any idea about what can be going wrong?

I am sorry! =/
bilal09 13-May-13 7:56am    
ghfhfgh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900