2011-05-17 100 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