sql statement ..., how can i resolve this error
-
i have made this sql statement and it works well but sometimes it show the result and sometimes not this sql statement between two datepicker it works with some date and the other no and sometime it gives error (out of range date) here is my code
cmd.CommandText = "select sum(fatrq1)reglz from reglz where fatrdate between '" + DateTimePicker1.Value + "'and'" + DateTimePicker2.Value + "'" 'fatrq1 dr = cmd.ExecuteReader
when i want to put these dates it doesn't works (from 1/1/2007 To 1/7/2007) it may work if i enter another dates what ca i do ?? -
i have made this sql statement and it works well but sometimes it show the result and sometimes not this sql statement between two datepicker it works with some date and the other no and sometime it gives error (out of range date) here is my code
cmd.CommandText = "select sum(fatrq1)reglz from reglz where fatrdate between '" + DateTimePicker1.Value + "'and'" + DateTimePicker2.Value + "'" 'fatrq1 dr = cmd.ExecuteReader
when i want to put these dates it doesn't works (from 1/1/2007 To 1/7/2007) it may work if i enter another dates what ca i do ??maybe the sql server doesn't accept this format for date. see http://sqljunkies.com/Article/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk I hope this help
-
i have made this sql statement and it works well but sometimes it show the result and sometimes not this sql statement between two datepicker it works with some date and the other no and sometime it gives error (out of range date) here is my code
cmd.CommandText = "select sum(fatrq1)reglz from reglz where fatrdate between '" + DateTimePicker1.Value + "'and'" + DateTimePicker2.Value + "'" 'fatrq1 dr = cmd.ExecuteReader
when i want to put these dates it doesn't works (from 1/1/2007 To 1/7/2007) it may work if i enter another dates what ca i do ??Use a parameterize query string to do this and you won't have to worry about what format the dates and times show up in in your string. The method that you used resulted in a string like this:
....WHERE fatrdate BETWEEN '#7/25/2007 12:24:31 PM#'and'...
which SQL Server doesn't like. Read Colin Angus Mackay's article on SQL Injection Attacks[^] and you'll find out why what you're doing doesn't work and how dangerous it is.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
maybe the sql server doesn't accept this format for date. see http://sqljunkies.com/Article/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk I hope this help
after editing my code like that
Dim x, y As String x = DateTimePicker1.Value y = DateTimePicker2.Value cmd.CommandText = "select sum(fatrq1)reglz, convert(varchar,fatrdate,101) from reglz where fatrdate between '" + x + "'and'" + y + "'" dr = cmd.ExecuteReader
the vb gives me error Column 'reglz.fatrdate' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.