Mike,
Well, first, you want to use a parameterized query. Once you have that,
you want to have your query look like this:
select
*
from
tblOffer
where
cast(floor(cast(OfferDate as float)) as datetime) =
cast(floor(cast(@date as float)) as datetime)
The above query will take the date passed in (in the form of the @date
parameter) and the OfferDate date time and trim the time from them. This
way, all the items which have the same date portion of the date time will be
the same and the comparison will work.
In on the off chance you are using the beta of SQL Server 2008, there is
a separate date data type which if you cast to it, I am pretty sure will
give you just the date parts to compare.
On a side note, I notice you are holding onto a connection
(aConnection). Is there are reason you don't just open a new one and then
close it when you are done? Connection pooling should eliminate the need
for something like this.
Additionally, even if you wanted to hold onto the connection, you are
not closing it after this method. Holding onto the connection while it is
open is a pretty bad idea, IMO.
--
- Nicholas Paldino [.NET/C# MVP]
-
(E-Mail Removed)
"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> I use MS SQL Express and VS 2005 c#, win application.
>
> I would like to select value rom DateTimePicker and list all values
> for selected date within GridView.
>
> I have method as follows:
>
> public DataTable GetOffersForDate_A(DateTime OfferDate)
> {
> DataTable dt = new DataTable();
> if (aConnection.State == ConnectionState.Closed)
> aConnection.Open();
> SqlDataAdapter da = new SqlDataAdapter();
>
> da.SelectCommand = new SqlCommand(@"SELECT * FROM tblOffer
> WHERE OfferDate = " + OfferDate, aConnection);
>
> da.Fill(dt);
> return dt;
> }
>
> Problem is, it is my conclusion, may be is wrong, in following:
>
> one value looks like:
> 26.11.2007 00:00:00
>
> and another one is something like this:
> 26.11.2007 07:35:23
>
> As we can see this is not the same time value. For me time is not
> important, I want to find only rows based on date selected in
> DateTimePicker.
>
> Furthermore, I am not sure that this is the best apporach.
>
> How I can do this?
>
> Thanks