连接到 sql、asp.net c# 的 javascript 出错

发布于 2024-11-07 16:36:19 字数 1063 浏览 0 评论 0原文

我有 javascript 计时器倒计时,代码如下:

<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()”:并非所有代码路径都会返回值。

当我尝试写 return 时,我遇到了另一个错误...这意味着我不知道该怎么做:)

I have javascript timer countdown with the code:

<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>

the code behind:

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); }
}

i get the error:
't.TargetDate()': not all code paths return value.

when i tried to write return i got another error... what means that i dont know what to do :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身骄傲 2024-11-14 16:36:20

首先,您必须根据函数规范以字符串形式返回目标日期值。
您应该读取数据表并将其返回。像这样的东西

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;
}

希望这有帮助。

First of all you must return the targetdate value as string according to your function specification.
You should read the datatable and return that. Something like this

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;
}

Hope this helps.

何以畏孤独 2024-11-14 16:36:19

你的方法应该返回字符串。事实并非如此。

public string TargetDate

没有返回

使用ExecuteScalar,而不是ExecuteReader

返回字符串值。

Your method supposed to return string. It doesn't.

public string TargetDate

There is no return.

Use ExecuteScalar, instead of ExecuteReader.

Return the string value.

十二 2024-11-14 16:36:19

您的连接字符串中还存在拼写错误:Trusted_Connectopn 应为 Trusted_Connection

You also have a spelling error in your connection string: Trusted_Connectopn should be Trusted_Connection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文