2011-05-17 103 views
1

我與編寫JavaScript代碼倒計時:錯誤連接到SQL,asp.net C#

<script language="JavaScript"> 
TargetDate = "<% = TargetDate %>"; 
/*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 language="JavaScript" src="countdown.js"></script> 
</script> 

後面的代碼:

public string TargetDate() 
{ 

    DataTable dataTable = new DataTable(); 
    using (SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connectopn=True")) 
    using (SqlCommand command = connection.CreateCommand()) 
    { 
     command.CommandText = "select * from endTime"; 
     connection.Open(); 
     SqlDataReader reader = command.ExecuteReader(); 
     dataTable.Load(reader); } 
} 

我得到的錯誤: 「T .TargetDate()':不是所有的代碼路徑都返回值。

當我試着寫了回報,我得到了另一個錯誤...什麼意思,我不知道該怎麼做:)

+0

您是否找到此解決方案? – 2011-05-25 18:56:22

回答

2

你應該返回字符串的方法。它沒有。

public string TargetDate

沒有return

使用ExecuteScalar而不是ExecuteReader

返回字符串值。

+0

@Bside - 是否有用? – 2011-05-21 01:36:05

0

你也有拼寫錯誤您的連接字符串中:Trusted_Connectopn應該是所有的Trusted_Connection

0

首先,你必須根據你的功能規格爲字符串返回targetdate值。 您應該閱讀數據表並返回。像這樣的東西

public string TargetDate() 
{ 
    String tDate = ""; 
    DataTable dataTable = new DataTable(); 
    using (SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True")) 
    using (SqlCommand command = connection.CreateCommand()) 
    { 
     command.CommandText = "select * from endTime"; 
     connection.Open(); 
     SqlDataReader reader = command.ExecuteReader(); 
     dataTable.Load(reader); 
     tDate = dataTable.Rows[0][0].toString(); // Expecting the first row and first column 
} 
return tDate; 
} 

希望這會有所幫助。

+0

謝謝。我得到錯誤的行:tDate = dataTable.rows [0] [0]; //期待第一行和第一列} return tDate;} 錯誤: CS1061:'System.Data.DataTable'不包含'rows'的定義,也沒有擴展方法'rows'接受第一個參數可以找到類型'System.Data.DataTable'(你是否缺少使用指令或程序集引用?) – Oshrib 2011-05-17 07:43:53

+0

再次感謝。我編輯:tDate = dataTable.Rows [0] [0] .ToString(); 現在,還有其他的問題...在aspx頁面,對於行:TargetDate =「<%= TargetDate%>」; 錯誤:CS1502:'System.IO.TextWriter.Write(char)'的最佳重載方法匹配有一些無效參數..... char?! ...哦,什麼是代碼..現在呢? :) 謝謝 ! – Oshrib 2011-05-17 08:06:27