Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

I have sqlDatareader that reads time from database and textbox that takes time from a form. I want to compare the two. textbox time is converted into TimeSpan, How do I convert the time from the reader to TimeSpan so i can compare the two using IF statement?
C#
TimeSpan starttime = TimeSpan.ParseExact(StartTime.Text, "HR:mm", CultureInfo.InvariantCulture);
TimeSpan endtime = TimeSpan.ParseExact(FinishTime.Text, "HR:mm", CultureInfo.InvariantCulture);


DateTime startTime = Convert.ToDateTime(reader["StartTime"]);
DateTime endTime = Convert.ToDateTime(reader["EndTime"]);



if     (startTime <= starttime && starttime <= endtime)
                {
                    
                    GridView1.Visible = false;


                }


I get error "The 'starttime' and 'endtime' is not in recognizable format." and also the if statement does not work as the 'startTime' is datetime and 'starttime' is TimeSpan.

Any suggestion please?
Thanks
Posted
Comments
[no name] 4-Jun-15 12:21pm    
Can you try this

TimeSpan startTime = TimeSpan.ParseExact(Convert.ToString(reader["StartTime"]), "HR:mm", CultureInfo.InvariantCulture);
TimeSpan endTime = TimeSpan.ParseExact(Convert.ToString(reader["EndTime"]), "HR:mm", CultureInfo.InvariantCulture);
Awoldeselassie 4-Jun-15 12:33pm    
Thanks for your answer mate, but that reads the new time from the textbox in the form. starttime = textbox called StartTime
startTime = needs to come from the reader, converted to TimeSpan.

So, I want to change this:-

DateTime startTime = Convert.ToDateTime(reader["StartTime"]);
DateTime endTime = Convert.ToDateTime(reader["EndTime"]);

which is data from the reader, to TimeSpan
[no name] 4-Jun-15 12:57pm    
Try this -

DateSpan startTime = Convert.ToDateTime(reader["StartTime"]).TimeOfDay;
DateSpan endTime = Convert.ToDateTime(reader["EndTime"]).TimeOfDay;

I assume you have label and checkbox control in gridview something like below -

<asp:gridview runat="server" id="GridView1" autogeneratecolumns="false" onrowdatabound="GridView1_RowDataBound" xmlns:asp="#unknown">
   <columns>
      <asp:templatefield>
          <itemtemplate>
              <asp:label runat="server" id="label" text="labelControl"></asp:label>
              <asp:checkbox runat="server" id="checkbox" />
          </itemtemplate>
      </asp:templatefield>
   </columns>
 </asp:gridview>


then you can access them as below -

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var label = e.Row.FindControl("labelControl") as Label;
        var checkBox = e.Row.FindControl("checkbox") as CheckBox;
    
        //Now set visibility on your condition here
        //label.Visible = true;
        //checkBox.Visible = true; 
    }
}


hope this helps.
 
Share this answer
 
v2
Comments
Awoldeselassie 5-Jun-15 10:54am    
spot on mate, thanks for you help, good progress today.
Try with below code

TimeSpan startTime = Convert.ToDateTime(reader["StartTime"]).TimeOfDay;
TimeSpan endTime = Convert.ToDateTime(reader["EndTime"]).TimeOfDay;
 
Share this answer
 
v2
Comments
Awoldeselassie 5-Jun-15 4:01am    
Thank you so much for that mate, it seems to be working, but I cant test it properly because now im getting another error. It is saying "Input string is not in the correct format" on the following line:

TimeSpan starttime = TimeSpan.ParseExact(StartTime.SelectedValue, "hh:mm", CultureInfo.InvariantCulture);
TimeSpan endtime = TimeSpan.ParseExact(FinishTime.SelectedValue, "hh:mm", CultureInfo.InvariantCulture);

I tried a few combination of the hh:mm format, but nothing worked, any idea?
Manoj Sawant 5-Jun-15 5:44am    
What if you remove TimeOfDay
DateTime startTime = Convert.ToDateTime(reader["StartTime"]);
DateTime endTime = Convert.ToDateTime(reader["EndTime"]);
Awoldeselassie 5-Jun-15 6:04am    
with the code above, it wont let me compare it with If statement because one will be 'System.DateTime' and the other 'System.TimeSpan'

with
TimeSpan startTime = Convert.ToDateTime(reader["StartTime"]).TimeOfDay;
TimeSpan endTime = Convert.ToDateTime(reader["EndTime"]).TimeOfDay;

I get a runtime error

Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.

I don't know what I am doing wrong, any suggestion please?
[no name] 5-Jun-15 8:22am    
You cannot compare it directly as both objects are different type here. Can you include some dummy input what you put in textbox control here?
Awoldeselassie 5-Jun-15 8:52am    
After struggling for a while, I just fixed it. I think the data in the reader does not need be converted.

DateTime startDate = Convert.ToDateTime(reader["StartDate"]);
DateTime endDate = Convert.ToDateTime(reader["EndDate"]);

TimeSpan startTime = (TimeSpan)reader["StartTime"];
TimeSpan endTime = (TimeSpan)reader["EndTime"];

then check for every possibility to avoid time overlap using IF,

if (((startDate <= startDateTime && startDateTime <= endDate)
|| (startDate <= endDateTime && endDateTime <= endDate)
|| (startDate >= startDateTime && endDateTime >= endDate))
&&
((startTime <= starttime && starttime <= endTime)
|| (startTime <= endtime && endtime <= endTime)
|| (startTime >= starttime && endtime >= endTime)))

{

GridView1.Visible = false;

}
else
{
GridView1.Visible = true;
}

Now, I just need to access the checkbox and label within the gridview to make one visible and hind the other depending on weather there is a time overlap or not. At the moment, im hiding the gridview just to see if its checking the time right. So rather than:
GridView1.Visible = false;

I would like to:

checkbox.visible = false;
label.visible = true;

checkbox and label are within the gridview, do you know how to?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900