Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » General » BindingSource.Filter problem with filtering by dates
       
Author Reply
Neven Draskovic
posted 66 posts
since Nov 18, 2011 
from

BindingSource.Filter problem with filtering by dates

  Posted on: 10 Feb 2012       
Hey

I use the filter method of BindingSource to filter it by one or multiple conditions, one of conditions being Date. First problem is that when I try to filter with this expression (e.g Date = #2/10/2012#) it returns no results (although there should be some). I've gone around that by making everything a date span like Date => #2/9/2012# AND Date<= #2/11/2012# (the date format is mm/dd//yyyy). This span will give me only the events that happened on 10th of February 2012 (it is like the filter ignores the '=' sign). This works properly for all dates except for the first and the last day of a month (because I programmatically subtract/add one day and make a span so for the first of the month it would be #2/0/2012# and that is an obvious error).

Do you know some way to solve this problem?
Thanks

Pravin Ghadge
posted  367 posts
since  Jun 23, 2010 
from 

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 10 Feb 2012        0  
hi Neven,

can u explain me why u r subtracting/adding 1 day in date.
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 11 Feb 2012        0  
Have you tried the exact same filter in the where clause of a SQL query that you execute directly somehow (not in your code)? My guess is that it is not a problem unique to the use of the BindingSource.Filter property.

Are you sure it is a date field without a time? The behavior you describe sounds like there is also a time with the date.
Thinking is a feeling; pleasant for some and unpleasant for others.
Neven Draskovic
posted  66 posts
since  Nov 18, 2011 
from 

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 11 Feb 2012        0  
@Sam: I took special care not to include time - I construct the query by adding Month,Day,Year properties of dateTimePicker to a string. I managed to "fix" the problem by making if-else statements (for first, last, and the rest of the days of the month) - this way I avoided the 0 and 32 dates in a month. But, while testing, one weird thing happened: When I need to filter the list to display only events on February 1st it includes events from January 31 (includes the date it should not), but when I want the results for February 2nd it displays proper results. Both use the same operator >= or <= but produce same results (I tried with < and >, I get the same results). Do you know what is causing this? 
P.S I tried my queries in SQL, they produce proper results

@Pravin: Like I mentioned in my question, I subtract and add a day because if I use the equal operator the query always returns empty set (like nothing happened that day), so I need to use a time interval (essentially saying: after Date1 but before Date2).
Pravin Ghadge
posted  367 posts
since  Jun 23, 2010 
from 

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 11 Feb 2012        0  
Neven,

can u paste ur code?
so that i can help u better.

Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 12 Feb 2012        0  
Yes, Neven I know that the query does not include time. I was asking about the database field, not the query. I am sorry I was not clear.

The best way to get the most relevant answer is to post something that recreates the problem. It helps to make that as small as possible that duplicates the problem. That sounds like a lot of work, but it is the type of thing that I have done before to get answers. And for this it should be quitre easy to do. Tell us how to create a table and give us a query and a small peice of code that we can use to see what is happening. Many times when I do that to get help, I solve the problem myself.

Pravin asked for the source code. You can post the source code as Pravin requested but it might be even more effective if you post a complete simple sample of the problem.
Thinking is a feeling; pleasant for some and unpleasant for others.
Neven Draskovic
posted  66 posts
since  Nov 18, 2011 
from 

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 13 Feb 2012        0  
First - the database field does contain the time parameter, but I cant change that, it is my clients database, so I have to work with what I have. 
Second, the code sample - there are 4 tables in my clients database that my application is using (not at the same time), each one contains informations about the work done by one type of their employees and my job is to make an application that generates reports for data from those tables (the tables do not have the same schema, and names of fields are not same). Naturally, they want to be able to filter that data and generate the report with filtered data. The filtering is done by either employee's ID, the workplace that they occupied at a particular time, date of that event and more -all in all, there are 5 different conditions that can be used to filter the     table(s) - every condition can be used individually or in combination with others. 
Now, since all other conditions work properly (they are simple numerical or textual values), except for the date condition, I'll post just the code that creates the date part of the query (the code is long so I uploaded it under name: "QueryBuilder").
As you can see, the checkBox checking adds the text of date condition to a list (Lista). Every condition has a corresponding checkBox that, when checked, adds a string item to the list that contains the text of a query. When user enters all the conditions he wants he clicks a button that creates the query that filters the BindingSource. This is the code that does that:
 
private void button5_Click(object sender, EventArgs e)
{
string upit = null;
upit = String.Join(" AND ", Lista.ToArray(), 0, Lista.Count);
textBox4.Text = upit;
Lista.Clear();


The textBox line is just so I can see the query before I execute it, it will be removed before distribution.
Lets say that user wishes to create the report that filters the BindingSource only by date (he selects the starting and ending date from 2 dateTimePicker's) and lets say he wants the report for February 1st 2012,this is a sample of a query that is created by clicking on the button:
 
(Pocetak >= #2/1/2012# AND Pocetak <= #2/2/2012#) //The same text is shown in TextBox
 
After that, the BindingSource is filtered by clicking on another button (again only because of testing purposes, when I'm satisfied with results, I'll just add the code that creates the query). This is a part of the code that handles generation of a report with filtered BindingSource:

RDS.Name = "KontrolorRadPojedinacni";
RDS.Value = (this.kontrolorRadPojedinacniBindingSource.Filter = textBox4.Text); \\ this line handles the filtering
this.reportViewer1.LocalReport.DataSources.Add(RDS);
this.reportViewer1.LocalReport.SetParameters(Ime);
this.reportViewer1.LocalReport.SetParameters(Prezime);
this.reportViewer1.LocalReport.SetParameters(ID);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Kontrolor.Izvjestaji.KontrolorRad(Pojedinacni).rdlc";

I think I gave you all the relevant informations, so please help me with this problem. Thanks again for your help and patience so far.
Pravin Ghadge
posted  367 posts
since  Jun 23, 2010 
from 

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 13 Feb 2012        1  
hi Neven,

try this,

  private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
           
            if (checkBox1.Checked)
            {
                if (NazivAktivneBaze == "KontrolorRad")
                {
 Lista.Add("(Pocetak between #" + Convert.ToDateTime( dateTimePicker1.Value.ToShortDateString()).ToString("MM/dd/yyyy") + "#  and
  #" + Convert.ToDateTime( dateTimePicker2.Value.ToShortDateString()).ToString("MM/dd/yyyy") + "#)");
}
}


Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 13 Feb 2012        0  
I think you did not give us all the relevant information. I think you did not tell us the most important; the one piece of information that could blow this case wide open.

What is the data type of the field in SQL Server? Please tell us that.
Thinking is a feeling; pleasant for some and unpleasant for others.
Neven Draskovic
posted  66 posts
since  Nov 18, 2011 
from 

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 14 Feb 2012        0  
Oh sorry, missed it - they are all datetime.
@Pravin - Ok I'll try that.
Thanks for your answers
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 14 Feb 2012        1  
Then that is the problem. If you want to check if a date is a certain day or before, you need to set the time to 23:59:59 (11:59:59 PM). Do you understand now? I expected that is what is happening but since you were overlooking the real cause, you did not realize what the real cause is and did not provide the relevant information.
Thinking is a feeling; pleasant for some and unpleasant for others.
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 14 Feb 2012        0  
Oh, and for what it is worth, I just got through posting a message in another web site's forum in which I called myself an idiot for overlooking something I should have known better. In this case, I assume you have not worked with comparing datetime data before now so it is understandable that you did not realize.
Thinking is a feeling; pleasant for some and unpleasant for others.
Neven Draskovic
posted  66 posts
since  Nov 18, 2011 
from 

 Re: BindingSource.Filter problem with filtering by dates
  Posted on: 14 Feb 2012        0  
Well I did, but in SQL, not in BindingSource.Filter - in SQL I would simply use BETWEEN. I tried it in my filter, the compiler throws me an error. I think I solved the problem the way Pravin suggested, but I still have some testing to do so I can't tell if that is the right answer or not. If you have some idea on how I could solve this more elegantly please share, I would appreciate every suggestion.

P.S Do you know how do I set time property of a Filter query - I tried #2/2/2012 23/59/59# it doesn't work?
       
Nevron Gauge for SharePoint
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved