在我的 TimeIn.aspx 文件中,我使用以下代码显示时钟:
<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>
时钟工作正常。然后在 TimeIn.aspx.cs 文件中,我写了 CheckIn 方法:
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");
}
在数据库中,CheckinTime
列的数据类型是Time(7)
。
当 CheckIn 事件触发时,它会在 TimeSpan.Parse 的第一行给出异常,因为 Label2.Text 有时间并添加了时间格式 (AM/PM)。
Label2.Text 的示例值为:1:41:28 PM
处理这种情况的最佳解决方案是什么?我真的很想在 sql server 中使用Time
数据类型,因为稍后我将不得不对时间字段执行计算。