Need to add time check to this query
The problem with your code is that you are passing only the date part to check to the SQL query. In order to make your query check both the date and time parts, you have to:
- Declare the SQL parameters of data type datetime(
SqlDbType.DateTime).
- The value you are passing to the sql parameter should be of data type
DateTime and contains both parts the date and time parts.
One way to achieve this is by using the same DateTimePciker to pass both date and time parts, then don't use the datetimepicker Text property and use DateTimePicker.Value property instead, it will give you both date and time parts:
SqlParameter fromParam= new SqlParameter("@from", SqlDbType.DateTime);
fromParam.Value = dateTimePicker1.Value;
SqlParameter toParam= new SqlParameter("@to", SqlDbType.DateTime);
toParam.Value = dateTimePicker2.Value;
commanddb.Parameters.Add(fromParam);
commanddb.Parameters.Add(toParam);
Or, by adding both the date part and time part coming from different datetimepickers to the same DateTime variable before passing it to the sql parameter. Something like this:
var datadb1 = DateTime.Parse(dateTimePicker1.Value.ToShortDateString());
var timedb1 = DateTime.Parse(dateTimePicker2.Value.ToShortTimeString());
DateTime datetimeCombined1 = datadb1 + new TimeSpan(timedb1.Hour,
timedb1.Minute,
timedb1.Second);
Then you have to pass this variable datetimeCombined1 to the SQL parameter, the same with the second datetime range, you have to combine both the parts before passing it.
This is assuming that you are using dateTimePicker1 to read the date part only and the dateTimePicker2 to read the time part only.
s my first sql proj. U mean using not 4 dateTimePickers but 2?