473,385 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Compare datetime value from datetimepicker with value within db

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
Nov 27 '07 #1
5 7611
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]
- mv*@spam.guard.caspershouse.com
"Mike" <ab******@yahoo.comwrote in message
news:e6********************************@4ax.com...
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

Nov 27 '07 #2
Nicholas.

This works, thank you very much.

I admit that I do not understand everything... These days I have a lot
of trouble with DateTime handling and conversion. As it seems, the
only solution (I hope is not) is to use parametrized queries on this
or on that way. I am confused because I do not like that approach.

Especially thanks for advice about closing connection, it was my
mistake nothing else. What you exactly mean when you say "Connection
pooling should eliminate the need for something like this"? Is there
some example which will guide me to learn better solution?

Thanks again.

Mike
On Tue, 27 Nov 2007 11:00:03 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comwrote:
>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.
Nov 27 '07 #3
Mike,

The parameterized query really had nothing to do with the solution. You
could have inserted the string representation of the date in place of @date
and it would have worked.

The reason you want to use a parameterized query is for security.
Google "injection attack" and it will show you why you should use them
instead of placing parameters in query strings yourself.

As for closing the connection, I'm assuming that aConnection is held on
the class level. You are keeping this open, which is generally a bad idea,
as you are wasting that database handle when you aren't using it. However,
it can be costly at times to create a new database handle each time you need
one. This is where connection pooling comes in. You enable it in your
connection string. Once you do that, just create and open a new connection
anywhere you need it, but make sure to call Close/Dispose on it when done
(use the using statement to ensure this happens).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike" <ab******@yahoo.comwrote in message
news:u3********************************@4ax.com...
Nicholas.

This works, thank you very much.

I admit that I do not understand everything... These days I have a lot
of trouble with DateTime handling and conversion. As it seems, the
only solution (I hope is not) is to use parametrized queries on this
or on that way. I am confused because I do not like that approach.

Especially thanks for advice about closing connection, it was my
mistake nothing else. What you exactly mean when you say "Connection
pooling should eliminate the need for something like this"? Is there
some example which will guide me to learn better solution?

Thanks again.

Mike
On Tue, 27 Nov 2007 11:00:03 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comwrote:
>>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.

Nov 27 '07 #4
Thanks Nicholas, you help me a lot.

Mike

On Tue, 27 Nov 2007 12:14:02 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comwrote:
>Mike,

The parameterized query really had nothing to do with the solution. You
could have inserted the string representation of the date in place of @date
and it would have worked.

The reason you want to use a parameterized query is for security.
Google "injection attack" and it will show you why you should use them
instead of placing parameters in query strings yourself.

As for closing the connection, I'm assuming that aConnection is held on
the class level. You are keeping this open, which is generally a bad idea,
as you are wasting that database handle when you aren't using it. However,
it can be costly at times to create a new database handle each time you need
one. This is where connection pooling comes in. You enable it in your
connection string. Once you do that, just create and open a new connection
anywhere you need it, but make sure to call Close/Dispose on it when done
(use the using statement to ensure this happens).
Nov 27 '07 #5
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Mike" <ab******@yahoo.comwrote in message
news:u3********************************@4ax.com...
Nicholas.

This works, thank you very much.

I admit that I do not understand everything... These days I have a lot
of trouble with DateTime handling and conversion. As it seems, the
only solution (I hope is not) is to use parametrized queries on this
or on that way. I am confused because I do not like that approach.
Why you do not like it? IMO it's much clearer code than a bunch of
concatenations.

The solution has nothing to do with it though. The magic was in the cast to
float and back to datetime:
cast(floor(cast(OfferDate as float)) as datetime)

That is a very clever solution, I did not know it :)
Nov 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dean L. Howen | last post by:
Dear friends, Could you help me to solve the problem: How can we set/get NULL values of datetime in DatetimePicker ? I design a form using DateTimePicker to input/output date value, but the date...
7
by: JJ | last post by:
Hi, What's the correct code for checking if a DateTime control is empty or not filled in with a date? Thanks, JJ
3
by: Brian | last post by:
Hello, I have a wierd problem. I have a datetime picker that doesn't seem to want to sinc up its .value and .text proberties. I can step through the code and see that they are different values. ...
9
by: Simon Harvey | last post by:
Hi all, I'm having a bit of a problem working with dates. My computer is british, but I'm developing an american application so I therefore need to use american dates. But I can't get my...
2
by: Ryan | last post by:
My form includes a field of the DateTime type. I want to allow the user to be able to set both the date and the time. By default the DateTimePicker shows just the date and calendar, the time is...
2
by: Kondapanaidu | last post by:
Hi, How to read selected date from Datetimepicker control in C#.NET2.0 Windows applications. Thanks in advance
11
by: Matt F | last post by:
I'm trying to do something that seems like it should be pretty simple, but haven't found a solution. I am trying to add a datacolumn to a datatable that adds or subtracts a number of days based on...
3
by: Yin99 | last post by:
I have the string "8/2/2007 12:00:00 AM" I'd like to get this in DateTime variable so I can manipulate (e.g. use TimeSpan to find difference). However, when I look at the DateTime constructors,...
25
by: Brian | last post by:
I have a datetimepicker formated for just time, the user selects the time. I want to compare if that time is between midnight and 8 am dtmTime #11:59:59 PM# and dtmTime < #08:00:00 AM# this...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.