locked
Dynamically binding dataset to the Report.rdlc RRS feed

  • Question

  • User-1870630656 posted

    Hi All,

    I am using vs 2010, I am creating a report.rdls with dataset (DatasetProducts.sxd). I want ot bind dataset dynamically to the report.

    Following is the code I am using. but its not working on button click.

    error was "A Data source instance has not been supplied for the data source 'ShowProductByCategory' "

    but after adding this line.

    ReportViewer1.Reset();

    it only shows the blank page.

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Microsoft.ApplicationBlocks.Data;
    using Microsoft.Reporting.WebForms;
    
    namespace WebApplication2
    {
        public partial class _Default : System.Web.UI.Page
        {
           
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                  string thisConnectionString =
       ConfigurationManager.ConnectionStrings[
       "WSS_ContentConnectionString"].ConnectionString;
             SqlParameter[] SearchValue = new SqlParameter[1];
                //ReportViewer1.Visible is set to false in design mode
                ReportViewer1.Visible = true;
                SqlConnection thisConnection = new SqlConnection(thisConnectionString);
                System.Data.DataSet thisDataSet = new System.Data.DataSet();
                SearchValue[0] = new SqlParameter("@CategoryName",
                                 TextBox1.Text);
    
                /* Put the stored procedure result into a dataset */
                thisDataSet = SqlHelper.ExecuteDataset(thisConnection,
                              "ShowProductByCategory", SearchValue);
    
                
                
                /*or   thisDataSet = SqlHelper.ExecuteDataset(thisConnection, 
                       "ShowProductByCategory", dropdownlist1.selectedvalue); 
                       if you only have 1 input parameter  */
    
                /* Associate thisDataSet  (now loaded with the stored 
                   procedure result) with the  ReportViewer datasource */
    
    		 ReportViewer1.Reset();
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportDataSource datasource = new 
                  ReportDataSource("DataSetProducts_ShowProductByCategory",
                  thisDataSet.Tables[0]);
                //datasource.Name = "DataSetProducts_ShowProductByCategory";
                //datasource.DataSourceId = "ObjectDataSource2";
                
               
                ReportViewer1.LocalReport.DataSources.Add(datasource);
                if (thisDataSet.Tables[0].Rows.Count == 0)
                {
                    //lblMessage.Text = "Sorry, no products under this category!";
                }
    
                ReportViewer1.LocalReport.Refresh();
            }
        }
    }
    

    i am also attaching the my aspx page code

     

    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" 
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)" 
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt">
            <LocalReport ReportPath="Report.rdlc">
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSource2" 
                        Name="ShowProductByCategory" />
                </DataSources>
            </LocalReport>
        </rsweb:ReportViewer>
        
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
            TypeName="WebApplication2.DataSetProductsTableAdapters.ShowProductByCategoryTableAdapter">
            <SelectParameters>
                <asp:Parameter DefaultValue="Audit" Name="CategoryName" Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>

     

     

     

     

    Thanks ,

    Monday, May 16, 2011 8:49 AM

All replies

  • User992473171 posted
    Go to the Report menu and select Data Sources.... This will show a list of the data sources that the report uses.
    Tuesday, May 24, 2011 3:56 AM
  • User254939732 posted

    Hey Man,

    Did u ever figure this out as I have the same problem?

    Thanks

    Tuesday, September 20, 2011 8:10 PM
  • User992473171 posted

    Yes I do

     

    Sunday, October 9, 2011 8:50 AM
  • User992473171 posted
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using Microsoft.Reporting;
    using Microsoft.ApplicationBlocks.Data;
    using Microsoft.Reporting.WebForms;
    using System.Drawing.Printing;
    using System.IO;
    
    public partial class _Default : System.Web.UI.Page
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
            string commandtext = "";
            //commandtext = Request.QueryString["cd"].ToString();
    
            commandtext = Session["cd"].ToString();  // My querry will be passed as session variable named "cd".
    
    
            Connct co = new Connct();
            SqlCommand com = new SqlCommand(commandtext, co.connect());
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = com;
            DataSet ds = new DataSet();
            adp.Fill(ds);
    
            //GridView1.DataSource = ds.Tables[0];
            //GridView1.DataBind();
    
            // Report Test
    
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
    
            thisDataSet = SqlHelper.ExecuteDataset(co.connect(), CommandType.Text, commandtext);
            ReportDataSource datasource = new ReportDataSource("DataSet_Report_Items", thisDataSet.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
                     
            ReportViewer1.LocalReport.DataSources.Add(datasource);
            ReportViewer1.LocalReport.Refresh();Here Is the Solution
    
     
    
    Sunday, October 9, 2011 8:51 AM
  • User992473171 posted

    http://www.microsoft.com/download/en/details.aspx?id=435

     

    Go to that Site And Download the Application , install It. You can find a dll in the bindirectory named Microsoft.ApplicationBlocks.Data;

    Add reference and use  the code that I posted Above.... It works fine for me.........

    Sunday, October 9, 2011 8:54 AM
  • User992473171 posted

    You have to create a XSD dataset  (DataSetProducts_ShowProductByCategory)

     and has to bind with the report And has to use the same name (DataSetProducts_ShowProductByCategory) in your code tooo

     

    Tuesday, October 11, 2011 5:01 AM