In my TimeIn.aspx File, I am displaying clock with following code:
<div>
<div>
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<br />
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Label runat="server" ID="Label4" Text="Current Time: "/>
<asp:Label runat="server" ID="Label2" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000" />
</div>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Check In" OnClick="CheckIn" />
</div>
The clock works fine. Then in TimeIn.aspx.cs file, I have written CheckIn method:
protected void CheckIn(object sender, EventArgs e)
{
TimeSpan currtime = TimeSpan.Parse(Label2.Text);
int eid = Convert.ToInt32(Session["EID"]);
DBClient = new DBConnection();
DBClient.CheckIn(eid, currtime, DateTime.Now.Date.ToString());
Response.Redirect("ECheckin.aspx");
}
In Database, the Datatype of CheckinTime column is Time(7).
When CheckIn event fires it gives exception on first line of TimeSpan.Parse because Label2.Text have time with time format added (AM/PM).
Sample value of Label2.Text is: 1:41:28 PM
Whats the best solution to deal with this situation? I really want to use Time datatype in sql server because later i will have to perform calculations on time field.
Timespan is basically difference between two times.
Or we can say a stopwatch where value in stopwatch is time that have elapsed since when clock started and when clock stopped.
It has nothing to do with AM or PM.
I guess the error that you are getting will be FormatException
Your format of Label Text is
DateTimethat’s why AM/PM.Instead of Timespan try using
DateTimeinstancelike