Solved

Filter a report by date range in Reportviewer

Posted on 2011-09-12
5
983 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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