How to connect DB and set SQL statement to the report viewer programmatically in C# RRS feed

  • Question

  • I designed rdlc, connected DB at design time.

    It shows report correctly.

    However I need to connect DB at runtime because DB location is not fixed.

    And I should set SQL statement at runtime as well.

    Where and How do I add code to do this?

    Sunday, September 1, 2019 2:52 AM

All replies

  • Hello,

    The following post explain this. The important thing to focus on is the data container rather than the actual connection.



    Also this should work for DataSet no matter how the DataSet was populated.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Sunday, September 1, 2019 3:07 AM
  • I designed rdlc, connected DB at design time.[...] I need to connect DB at runtime because DB location is not fixed.

    When you use the ReportViewer to present a local report (.rdlc), contrary to what happens when you present a server report (.rdl), the data for the report is always generated at runtime. The .rdlc (contrary to the .rdl) does not allow the database connection to be embedded in the report definition. So the connection is always in your source code. If you assigned it at design time, what the designer did was that it added some lines to your program to create and populate a dataset, and then connected that dataset to the ReportViewer.

    So, find in your source the lines added by the designer where it builds the dataset. You can then change them so that you pass your own connection instead of using the one that the designer used. You can also modify the dataAdapter that populates the dataset, and change the selectCommand.CommandText to use any SQL statement that you wish (as long as the column names are the same, otherwise they will not match the columns in the report definition and the report will not work).

    Sunday, September 1, 2019 1:34 PM
  • Hi Jeff0803,

    Thank you for posting here.

    You could look at the following link to connect DB to the report viewer.

    Building Reports from a DataSet using ReportViewer

    Note:This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 2, 2019 7:34 AM