2012-06-06 44 views
0

在我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數據類型,因爲以後我將不得不在時間字段上執行計算。

+0

將quizti2.Text/Database中的值放入questi上。 –

+0

'在數據庫中,CheckinTime的數據類型列是Time(7).'。手段?? –

+0

@Nikhil Agrawal我正在使用Sql Server 2008作爲後端數據庫。在我的應用程序數據庫表中,有一列CheckinTime,其數據類型是Time(7)。我想在該字段中存儲簽入時間。 – Azeem

回答

3

Timespan基本上是兩次相差。

或者我們可以說一個秒錶,其中秒錶中的值是從時鐘開始和時鐘停止以來已經過去的時間。

它與AM或PM無關。

Timespan = Date1 - Date2 

我想您得到將是錯誤FormatException

你的標籤文本的格式爲DateTime這就是爲什麼AM/PM。

而是時間跨度的請嘗試使用DateTime例如

DateTime currtime = DateTime.Parse(Label2.Text); 
+0

謝謝,我會盡力回覆你。 – Azeem

+0

我懷疑你的陳述「Timespan基本上是兩次差異」。 Timespan是一個代表時間間隔的結構......就是這樣。你的陳述導致了這樣的看法,即時間跨度只能保持我認爲是錯誤的差異。 – ABH

+0

您的評論支持我並不違反我。你說'Timespan是代表時間間隔的結構'。這就是我所說的。時間間隔只是差異。我選擇了差異。您選擇了間隔。同樣的事情。名稱是時間跨度,意思是兩次之間的跨度。 –

0

你可以做添加時間跨度爲DateTime像下面

TimeSpan timespan = new TimeSpan(03,00,00); 
    DateTime time = DateTime.Today.Add(timespan); 
    string displayTime = time.ToString("hh:mm tt"); 

,而不是03,00,00,你可以直接傳遞您的時間跨度變量