2011-05-22 92 views
0

我正在嘗試用javascript,sql,c#,asp.net編寫定時器倒計時代碼。定時器倒計時sql,asp.net c#,javascript

現在的代碼是這樣的:

<input type="text" id="auctionEndDate" value="<%=TargetDate()%>" style="display:none"/> 
<script language="JavaScript" type="text/javascript"> 


TargetDate = "document.forms[0].auctionEndDate.value"; 
/*this is a property in code behind*/BackColor = "palegreen"; 
ForeColor = "navy"; 
CountActive = true; 
CountStepper = -1; 
LeadingZero = true; 
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds."; 
FinishMessage = "the auction end" 
</script> 
<script type="text/javascript" language="JavaScript" 
src="http://scripts.hashemian.com/js/countdown.js"></script> 
</script> 

CS文件:

public string TargetDate() 
{ 
    SqlConnection connection = new SqlConnection("Data Source=tcp:***.*****.com;Initial Catalog=DB_***_****;User ID=***_*****_****_user;Password=******;Integrated Security=False;"); 
    string commandtext = "SELECT endTime FROM Timer"; 
    SqlCommand command = new SqlCommand(commandtext, connection); 
    connection.Open(); 
    string tDate = (string)command.ExecuteScalar(); 
    connection.Close(); 
    return tDate; 
} 

定時器DESGIN表:

=============== 

id 

endTime (nvarchar (150) 

行結束時間:12/31/2020 5: 00 AM

和結果:NaN Days,NaN小時,NaN分鐘,NaN秒。

當我將nvarchar(150)的設計更改爲datetime時出現錯誤:無法將類型爲「System.DateTime」的對象轉換爲鍵入「System.String」。

爲什麼我得到:NaN Days,NaN Hours,NaN Minutes,NaN Seconds。

問題是什麼?謝謝。

回答

0

要開始,你需要改變

TargetDate = "document.forms[0].auctionEndDate.value"; 

TargetDate = document.forms[0].auctionEndDate.value; 

什麼,我需要做的,以避免錯誤,當我從nvarchar的類型更改爲日期時間?

以猜測在這裏,因爲我真的不是一個.NET程序員,你需要改變

string tDate = (string)command.ExecuteScalar(); 

DateTime tDate = (DateTime) command.ExecuteScalar(); 

You can call GetType() on the return value ExecuteScalar() to see what type it really is.

+0

賓果!!!!喝彩! – Oshrib 2011-05-22 14:42:20

+0

我需要做什麼來避免錯誤,當我從nvarchar類型更改爲datetime? – Oshrib 2011-05-22 15:12:05

+0

@Bside看到我的編輯。 – 2011-05-22 15:15:30