Solved

Filter a report by date range in Reportviewer

Posted on 2011-09-12
5
856 Views
Last Modified: 2012-05-12
I want to produce a report filtered by date range using microsofts report viewer

I have tried the following code which doesn't work. It returns an error saying incorrect number of parameters for CONVERT function.  I have used the label control to view the string passed to the filter expression and it looks correct.  Can anyone advise how to filter reportviewer reports by date range.

 protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
       
        Label20.Text = "[Date_SIF_Entered] > CONVERT(DATETIME," + TextBox1.Text + ",103) and [DATE_SIF_ENTERED] < CONVERT(DATETIME," + TextBox2.Text + ",103) ";
        ObjectDataSource4.FilterExpression = "[Date_SIF_Entered] > CONVERT(DATETIME," + TextBox1.Text + ",103) and [DATE_SIF_ENTERED] < CONVERT(DATETIME," + TextBox2.Text + ",103)";
        SifEntryReportByDateRange.LocalReport.Refresh();
    }
0
Comment
Question by:DCMBS
  • 4
5 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 36528002
Remove quotes " around TextBox.Text in the convert function. It returns string already.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23020764.html may help you.
0
 
LVL 9

Author Comment

by:DCMBS
ID: 36529144
I have had a look at the link and I have come up with the following.

I have an objectdatassource with two parameters of type datetime, FromDate and ToDate.

The report produces a report showing all records when no values are  defined for the parameters.

I have two Textboxes to allow the dates to be entered Textbox1 and Textbox2

I have created the following event handler for the on Text_Changed event of Textbox2.  The labels are just to let me inspect what is happening

protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        ObjectDataSource4.FilterExpression = "Date_SIF_Entered >= ' #{0}# ' and Date_SIF_Entered < ' #{1}#' ";
        Label20.Text = ObjectDataSource4.FilterExpression.ToString();
        Label21.Text = TextBox1.Text;
        Label22.Text = TextBox2.Text;
       
        /*SifEntryReportByDateRange.LocalReport.Refresh();*/
       
        ObjectDataSource4.FilterParameters["FromDate"].DefaultValue = TextBox1.Text;
        ObjectDataSource4.FilterParameters["ToDate"].DefaultValue = TextBox2.Text;
        //ObjectDataSource4.Select(DataSourceSelectArguments.Empty);
        SifEntryReportByDateRange.LocalReport.Refresh();
    }

This does not succeed in filtering the report.  Any help in working out why would be appreciated.
0
 
LVL 9

Author Comment

by:DCMBS
ID: 36529211
Abort that the above code works.  It requires the date in mm/dd/yyyy format though, which is not good.  I really need to=he date to be entered in dd/MM/yyyy format.
0
 
LVL 9

Author Comment

by:DCMBS
ID: 36529377
I have come up with the following for now.  Any further suggestions are welcome.  The date is entered as dd/mm/yyyy and then rearranged to US format mm/dd/yyyy

ObjectDataSource4.FilterExpression = "Date_SIF_Entered >= ' #{0}# ' and Date_SIF_Entered < ' #{1}#' ";
               
        String[] TempFromDate = TextBox1.Text.Split('/');        String[] TempToDate = TextBox2.Text.Split('/');
        String USToDate = TempToDate[1] + '/' + TempToDate[0] + '/' + TempToDate[2];  
       
        ObjectDataSource4.FilterParameters["FromDate"].DefaultValue = USFromDate;

        String USFromDate = TempFromDate[1] + '/' + TempFromDate[0] + '/' + TempFromDate[2];
           
        ObjectDataSource4.FilterParameters["ToDate"].DefaultValue = USToDate;
       
        SifEntryReportByDateRange.LocalReport.Refresh();
0
 
LVL 9

Author Closing Comment

by:DCMBS
ID: 36529384
The link pointed me to the solution.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now