Link to home
Start Free TrialLog in
Avatar of Umut Şeker
Umut ŞekerFlag for Türkiye

asked on

Using Session DateTime Variable as SqlDataSource Parameter

I'm assigning DateTime data (for example: 01.01.2007 and 01.01.2009) to  session veriables like;
                Session.Add("FILTER_BEGINDATE", Convert.ToDateTime(txt_BeginDate.Text));
                Session.Add("FILTER_ENDDATE", Convert.ToDateTime(txt_EndDate.Text));

and using them as parameters to a SqlDataSource like;
WHERE  (MYDATE > @FILTER_BEGINDATE) AND (MYDATE < @FILTER_ENDDATE)

...@FILTER_BEGINDATE AND @FILTER_ENDDATE are DateTime parameters.

But I could get and data from the SqlDataSource. What could be the problem?
I'm using ASP.Net 2005 with C#
Thanks,
ASKER CERTIFIED SOLUTION
Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Umut Şeker

ASKER

MYDATE is a datetime data in sql server.
204 is the style of "dd.mm.yyyy".

For example if I write a sql statement like that;
Select...... CONVERT(datetime, '01.01.2019', 204) AS Expr1..

or

if I use a WHERE close like that;
WHERE
(CONVERT(datetime, MYDATE, 204) > CONVERT(datetime, @FILTER_BEGINDATE, 204))
AND
(CONVERT(datetime, MYDATE, 204) < CONVERT(datetime, @FILTER_ENDDATE, 204))

The error is;
Conversion failed when converting datetime from character string.
When I use the way srivatsavaye told like;
WHERE
(CONVERT(varchar, MYDATE, 204) > CONVERT(varchar, @FILTER_BEGINDATE, 204))
AND
(CONVERT(varchar, MYDATE, 204) < CONVERT(varchar, @FILTER_ENDDATE, 204))

there is no erro but no data came as the date interval is true..
ohh what a mistake I made... :( the field's type is varchar.
So I solve this problem like;
 (CONVERT (datetime, CONVERT (varchar(10), MYDATE, 104), 104) > CONVERT (datetime, @FILTER_BEGINDATE, 104)) AND (CONVERT (datetime, CONVERT (varchar(10), MYDATE, 104), 104) < CONVERT (datetime, @FILTER_ENDDATE, 104))
Thanks,