I am getting the following error “The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.” and the date format is “DD/MM/YY”
public DataSet GetSynchronizeDataForDLDocs(string date_pushed)
{
DataSet ds = new DataSet();
string strQuery=string.Empty;
if (date_pushed != null && date_pushed != string.Empty) // 105
strQuery =
@"select
a.DLNO,
a.NAME,
b.TOPSTRING,
Convert(datetime,a.DOB,103) as DOB,
Convert(datetime,a.DOI,103) as DOI,
Convert(datetime,b.datepushed,103) as datepushed
from
PLInfo_Demo a,
DLInfo_Demo b
where
a.dlno=b.dlno
and
Convert(DateTime,b.datepushed,103) > CONVERT(varchar,'" + date_pushed + @"' ,103)
and
DATEPART(hh, b.datepushed) > Datepart(hh,'" + date_pushed + @"')
and
DATEPART(MI, b.datepushed) > Datepart(MI,'" + date_pushed + @"' )
and
DATEPART(ss, b.datepushed) > Datepart(ss,'" + date_pushed + @"' )
order by b.datepushed desc";
else
strQuery = @"
select
a.DLNO,
a.NAME,
b.TOPSTRING,
Convert(datetime,a.DOB,103) as DOB,
Convert(datetime,a.DOI,103) as DOI,
Convert(datetime,b.datepushed,103) as datepushed
from
PLInfo_Demo a,
DLInfo_Demo b
where
a.dlno=b.dlno ";
ds = DbManager.ExecuteSalarData(
strQuery,
CommandType.Text,
DbManager.strcon2,
null);
return ds;
}
First thing would be do not pass in
date_pushedas a string. Parse it in your c# (DateTime.Parseor similar, perhaps specifying a format and culture), and pass it as a parameter. In all the places you have'" + date_pushed + '"', use@theParameterinstead.The next thing is to store
b.datepushedas adatetime– there should be no need to useconvertonb.datepushed. If it is a string you are doing it wrong.After that, you are comparing a
datetimefield to adatetimeparameter, which will work without issue. For example:becomes
where
b.datepushedis thedatetimefield, and@date_pushedis thedatetimeparameter. You can of course useDATEPARTetc with adatetime– the important point is: it isn’t parsing anything at all.