none
connect a Dataset created in code with ReportViewer RRS feed

  • Question

  • I have written code in vb.net 2008 to create a dataset and fill it using a dataadapter with SQL string.
    I am not using any wizards to build any datasources or datasets.

    I have been trying to figure out how to get the MicrosoftReportViewer to see my dataset.  Is this possible?

    I have used ActiveReports in the past, but not much.
    I am new to this stuff.  Can someone explain carefully the steps to design the report and attach it to the dataset that I built?

    I really need details if someone can spare the time.

    -duane

    Duane Sanders
    • Edited by Duane Sanders Wednesday, August 12, 2009 7:16 PM correction
    Wednesday, August 12, 2009 7:15 PM

Answers

  • Hi Duane,

    If you already have a dataset in your project and it is hooked up to the database via TableAdapter, then it is straightforward.  You can simply do this:

    1.  Add New Item.  Choose Report under Reporting tab.  Let's call it Report1.rdlc
    2.  Open the report.  Your dataset should be in the data source pane.
    3.  Design your report.  http://msdn.microsoft.com/en-us/library/ms252067.aspx  Report1.rdlc
    4.  Add the viewer to your form.
    5.  Click on the ReportViewer Task Pane and configure the viewer to use Report1.rdlc.
    6.  Run the app and you should be all set.

    However, from what I understand, you have created the dataset in code.

    If you create a dataset in code programmatically, it is better to create a "placeholder" XSD in the same schema so that the RDLC designer knows the schema of the dataset and you can use it to design the report.

    Here are the steps that I would do to add a report to my app:
    0.  Create a Dataset1.xsd, which a schema matching the one that you have created in code
    1.  Add New Item.  Choose Report under Reporting tab.  Let's call it Report1.rdlc
    2.  Open Report1.rdlc in the RDLC designer.
    3.  The dataset should show up in the Data Sources Pane.
    4.  You can add a table to the report and can drag the fields form your dataset into the table.  http://msdn.microsoft.com/en-us/library/ms252067.aspx


    5.  Then I would add the viewer to the form and then configure it to user Report1.rdlc.

    And here is the fun part of hooking up the in-memory dataset to the ReportViewer.  After step #5, you will notice that that binding sources have automatically generated for the dataset.

    Assume that the table that you are using in the report is DataTable1.  The binding source name generated is DataTable1BindingSource.

    You might want to look at the automatically generated code under Form1.Designer.cs under the // ReportViewer1 section to understand what's under the hood.  It should look something like this:

    reportDataSource1.Name = "DataSet1_DataTable1";

    reportDataSource1.Value = this.DataTable1BindingSource;

    this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);

    this.reportViewer1.LocalReport.ReportEmbeddedResource = "DynamicDataSet.Report1.rdlc";

     

    Essentially, it basically binds the DataTable1BindingSource to the DataSet1_DataTable1 in your Report1.rdlc.

    Also, the auto-generated code configure the DataTable1BindingSource to DataSet1 -> DataTable1, which is just a placeholder because you have created the dataset in code already.  (Scroll down in the Form1.Designer.cs.)

    //

    // DataTable1BindingSource

    //

    this.DataTable1BindingSource.DataMember = "DataTable1";

    this.DataTable1BindingSource.DataSource = this.DataSet1;

    What you would need to do to is to change this point to the dataset and data table that you have created dynamically in code.

    I hope that this help.

    Thanks.

    Stella Chan
    Program Manager
    SQL Server Reporting Services

    ---------------------

    This code is provided as-is with no support or warranty.

    Saturday, August 15, 2009 12:43 AM

All replies

  • Not sure if this will help, but check out the ReportDataSource class and see if you can hook it up that way. Good Luck!
    Thursday, August 13, 2009 3:43 PM
  • Hi Duane,

    If you already have a dataset in your project and it is hooked up to the database via TableAdapter, then it is straightforward.  You can simply do this:

    1.  Add New Item.  Choose Report under Reporting tab.  Let's call it Report1.rdlc
    2.  Open the report.  Your dataset should be in the data source pane.
    3.  Design your report.  http://msdn.microsoft.com/en-us/library/ms252067.aspx  Report1.rdlc
    4.  Add the viewer to your form.
    5.  Click on the ReportViewer Task Pane and configure the viewer to use Report1.rdlc.
    6.  Run the app and you should be all set.

    However, from what I understand, you have created the dataset in code.

    If you create a dataset in code programmatically, it is better to create a "placeholder" XSD in the same schema so that the RDLC designer knows the schema of the dataset and you can use it to design the report.

    Here are the steps that I would do to add a report to my app:
    0.  Create a Dataset1.xsd, which a schema matching the one that you have created in code
    1.  Add New Item.  Choose Report under Reporting tab.  Let's call it Report1.rdlc
    2.  Open Report1.rdlc in the RDLC designer.
    3.  The dataset should show up in the Data Sources Pane.
    4.  You can add a table to the report and can drag the fields form your dataset into the table.  http://msdn.microsoft.com/en-us/library/ms252067.aspx


    5.  Then I would add the viewer to the form and then configure it to user Report1.rdlc.

    And here is the fun part of hooking up the in-memory dataset to the ReportViewer.  After step #5, you will notice that that binding sources have automatically generated for the dataset.

    Assume that the table that you are using in the report is DataTable1.  The binding source name generated is DataTable1BindingSource.

    You might want to look at the automatically generated code under Form1.Designer.cs under the // ReportViewer1 section to understand what's under the hood.  It should look something like this:

    reportDataSource1.Name = "DataSet1_DataTable1";

    reportDataSource1.Value = this.DataTable1BindingSource;

    this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);

    this.reportViewer1.LocalReport.ReportEmbeddedResource = "DynamicDataSet.Report1.rdlc";

     

    Essentially, it basically binds the DataTable1BindingSource to the DataSet1_DataTable1 in your Report1.rdlc.

    Also, the auto-generated code configure the DataTable1BindingSource to DataSet1 -> DataTable1, which is just a placeholder because you have created the dataset in code already.  (Scroll down in the Form1.Designer.cs.)

    //

    // DataTable1BindingSource

    //

    this.DataTable1BindingSource.DataMember = "DataTable1";

    this.DataTable1BindingSource.DataSource = this.DataSet1;

    What you would need to do to is to change this point to the dataset and data table that you have created dynamically in code.

    I hope that this help.

    Thanks.

    Stella Chan
    Program Manager
    SQL Server Reporting Services

    ---------------------

    This code is provided as-is with no support or warranty.

    Saturday, August 15, 2009 12:43 AM
  • Stella,

    Thanks for the info.
    I assume it is normal for the forms designer to fuss at me after I change these lines?????

    -duane
    Duane Sanders
    Monday, August 24, 2009 8:31 PM
  • Hi Duane,
    Normally, I do not make those changes in the Form.Designer.cs file.  I usually put the changes in Form_Load or later.  That way, the form designer will not fuss.

    Thanks.

    Stella Chan
    Program Manager
    SQL Server Reporting Services



    Tuesday, August 25, 2009 4:30 AM
  • Stella,

    Thanks.  That works very well.

    -duane
    Duane Sanders
    Thursday, August 27, 2009 1:35 PM
  • Hi Duane,

    If you already have a dataset in your project and it is hooked up to the database via TableAdapter, then it is straightforward.  You can simply do this:

    1.  Add New Item.  Choose Report under Reporting tab.  Let's call it Report1.rdlc
    2.  Open the report.  Your dataset should be in the data source pane.
    3.  Design your report.  http://msdn.microsoft.com/en-us/library/ms252067.aspx  Report1.rdlc
    4.  Add the viewer to your form.
    5.  Click on the ReportViewer Task Pane and configure the viewer to use Report1.rdlc.
    6.  Run the app and you should be all set.

    However, from what I understand, you have created the dataset in code.

    If you create a dataset in code programmatically, it is better to create a "placeholder" XSD in the same schema so that the RDLC designer knows the schema of the dataset and you can use it to design the report.

    Here are the steps that I would do to add a report to my app:
    0.  Create a Dataset1.xsd, which a schema matching the one that you have created in code
    1.  Add New Item.  Choose Report under Reporting tab.  Let's call it Report1.rdlc
    2.  Open Report1.rdlc in the RDLC designer.
    3.  The dataset should show up in the Data Sources Pane.
    4.  You can add a table to the report and can drag the fields form your dataset into the table.  http://msdn.microsoft.com/en-us/library/ms252067.aspx


    5.  Then I would add the viewer to the form and then configure it to user Report1.rdlc.

    And here is the fun part of hooking up the in-memory dataset to the ReportViewer.  After step #5, you will notice that that binding sources have automatically generated for the dataset.

    Assume that the table that you are using in the report is DataTable1.  The binding source name generated is DataTable1BindingSource.

    You might want to look at the automatically generated code under Form1.Designer.cs under the // ReportViewer1 section to understand what's under the hood.  It should look something like this:

    reportDataSource1.Name = "DataSet1_DataTable1";

    reportDataSource1.Value = this.DataTable1BindingSource;

    this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);

    this.reportViewer1.LocalReport.ReportEmbeddedResource = "DynamicDataSet.Report1.rdlc";

     

    Essentially, it basically binds the DataTable1BindingSource to the DataSet1_DataTable1 in your Report1.rdlc.

    Also, the auto-generated code configure the DataTable1BindingSource to DataSet1 -> DataTable1, which is just a placeholder because you have created the dataset in code already.  (Scroll down in the Form1.Designer.cs.)

    //

    // DataTable1BindingSource

    //

    this.DataTable1BindingSource.DataMember = "DataTable1";

    this.DataTable1BindingSource.DataSource = this.DataSet1;

    What you would need to do to is to change this point to the dataset and data table that you have created dynamically in code.

    I hope that this help.

    Thanks.

    Stella Chan
    Program Manager
    SQL Server Reporting Services

    ---------------------

    This code is provided as-is with no support or warranty.


    Hi Stella
    I would like to know one thing, I have a Datatable generated dynamically and I want  to generate report based on the DataTable. The fields of Data table varies with users slection. So I can't know the schema of the Datatable in advace.
    In this situation how can I generate a report from the DataTable?
    Please give your valuable suggestion. I am new to this area.
    Any help would be greatly appreciated.
    Regards
    swapna

    Thanks & Regards
    Swapna
    Monday, September 21, 2009 8:25 PM
  • I am getting ur point, as I already have done Generic Reporting using RDLC.

    Know one thing clearly :

    You must have three things to view report dynamically :
    1) MS ReportViewer
    2) RDLC
    3) XSD file.

    But, the XSD's schema will be fixed if reports are not dynamic. So, You are stuck basically with XSD.

    What I figured out is that RDLC's XML Schema contains XSD definition.

    So, if your data changes at runtime, you need to create a generic class which designs RDLC at runtime with given DataSet's description.

    For more info you should design one report using wizard, and read RDLC with XML editor.

    You will undersatnd what I am telling.

    I have learned and built it on my way...it works fantastic, but I cannot share the code...can only suggest what I did.

    What I do not like about ReportViewer, is that it does not have any library like Crystal Reports provide.

    Everything is fixed i.e. Column widths, Rendering Pattern - all which is defined in RDLC schema itself.

    This restricts programmers to generically modify the report layouts.


    Dhruv Patel
    [ Email : mail2dhruv@rocketmail.com ]
    Please Mark As Answer if you think the answer was correct.
    Thursday, October 29, 2009 2:29 PM
  • Thanks Dhruv
    Best Regards
    Swapna

    • Proposed as answer by KevalSavani Thursday, July 3, 2014 9:57 AM
    • Unproposed as answer by KevalSavani Thursday, July 3, 2014 9:57 AM
    Thursday, March 4, 2010 12:32 AM
  • HI, Guys

    I am new at doing reports in VB.NET 2010. I saw so many tutorial on youtube which explains how to create reports in VS2010 using reportviewer, datasource etc. I saw that connection made by Datasource and then Fields are placed on report, then Create new form, place report viewers then select report from report source etc etc etc .

    Basically tutorial explains how to create by report in  VS by data source tools but what if I have to move or change path of Database (I use MS access for database).

    I am able to do everything to generate report but i want dynamic connection string for report viewer, I am NOT using crystal report

    How do I have database path @ run time, is there any link or video tutorial would be very help full.

    Thursday, July 3, 2014 9:58 AM
  • If you create a dataset in code programmatically, I found that the constructor must construct the datatable. The report wizard seems to key off of this to give you the available fields to then construct the report. Also, don't forget to set the Copy to output directory to "copy always". Then make sure you build the project after creating the class, in order for the report wizard or just report to see the dataset object.

    Scott Hoffman

    Sunday, October 15, 2017 5:45 PM