Creating a ReportDataSource form Xml Schema RRS feed

  • Question

  • I need to create the ReportDataSource (for a Microsoft Report) object from an xmlSchema during runtime.

    I can get the data to show if i use a DataSet created in the project itself: e.g.

    ReportDataSource ds = new ReportDataSource("DataSet1_MostRuns");
    ds.Value = new DataSet1TableAdapters.MostRunsTableAdapter().GetData();

    but i need to load the dataset at runtime and change the connection string as well if possible.

    When i've tried this so far the schema loads but the tables contain no rows.

    How do i get the DataSet to populate the rows?

    And also how do i change/set the connection string?

    Any help would be appreciated. Thanks

    • Moved by Chao Kuo Tuesday, July 27, 2010 10:07 AM better fit (From:Visual C# General)
    Sunday, July 25, 2010 10:47 AM

All replies

  • Hi Thilina..

    To Load DataSet or DataSource at runtime just follow bellow lines

    1) First of all clear your DataSources



    2) Then add your DataSources in your Report.



    .reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_MostRuns",DatasourceValue));

    DatasourceValue means your actucal data.(example DataTable,list,collection etc)

    3) Collapsed Document.


         this.reportViewer1.DocumentMapCollapsed = true;

    4) Refresh Report





    To Change ConnectionString at RunTime the follow this

    In your project there is one file named app.config where connection string will be stored. the format of that file is xml.

    so you can read this file using xmlReader, change connectionstring and save file.


    app.config File (Your file looks like this)



    xml version="1.0" encoding="utf-8" ?>















    add name="ConnectionString1"



    connectionString="Data Source=.;Initial Catalog=DataBaseName;Integrated Security=True"



    providerName="System.Data.SqlClient" />







    XmlReader reader = XmlReader.Create(filePath, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Auto });
    var elements = ReadElements(reader);
     private static IEnumerable<XElement> ReadElements(XmlReader reader)
          List<XElement> ex = new List<XElement>();
          while (reader.Read())
            switch (reader.NodeType)
              case XmlNodeType.Element:
                if (reader.Name == "ConnectionStrings")
                  XElement element = XElement.ReadFrom(reader) as XElement;
          return ex;

    ReadElements Method will give you List<XElement>  now you can change the XElement and Save.

     private static void ChangeConnectionString(IEnumerable<XElement> elements)
          XNamespace xnsp = "";      
     foreach (var element in elements)
             element.Element("add").Attribute("ConnectionString").Value = "YourConnectionString";

    Now Replace your Xelement and Save your file as app.config and replace with old file.

    If you have any question Then Feel free to ask.

    Happy Codding.......

    Wednesday, July 28, 2010 12:30 PM