4

如果检索到的日期小于当前日期,我想停用 button6,我为此使用了以下代码,但它不起作用。请帮我找出错误。

protected void Button6_Click1(object sender, EventArgs e)
{
    MySqlConnection connection = new MySqlConnection("server=localhost; database=e-learningsystem; uid=root; password=123;port=3307;");
    connection.Open();
    try
    {
        MySqlCommand cmd = new MySqlCommand("SELECT Date FROM fundamentals of is WHERE ChapNo=Chapter 1", connection);
        string date = Convert.ToString(cmd.ExecuteScalar());
        //date = cmd;
        if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)
        {
            DownLoadFileFromServer("~/NewFolder1/" + "Fundamentals of IS.pdf");
        }
        else
        {
            Button6.Enabled = false;
        }
    }  
    catch (Exception ex)
    {
        // file IO errors

    }
}

这是serverMapPath代码

public static string ServerMapPath(string path)
{
    return HttpContext.Current.Server.MapPath(path);
}

public static HttpResponse GetHttpResponse()
{
    return HttpContext.Current.Response;
}
public static void DownLoadFileFromServer(string fileName)
{
    //This is used to get Project Location.
    try
    {
        string filePath = ServerMapPath(fileName);
        //This is used to get the current response.
        HttpResponse res = GetHttpResponse();
        res.Clear();
        res.AppendHeader("content-disposition", "attachment; filename=" + filePath);
        res.ContentType = "application/octet-stream";
        res.WriteFile(filePath);
        res.Flush();
        res.End();
    }
    catch (Exception ex)
    {

    }
}
4

8 回答 8

1

MSDN 答案:

我认为这会对你有所帮助

int result = DateTime.Compare(date1, date2);

string relationship;
if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";
于 2013-11-01T10:29:43.597 回答
0

以下行可能存在逻辑错误,因为您将cmd变量转换为 aDateTime而不是date命令返回的变量:

  if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)

改用它可能会解决您的问题:

  if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) < 0)
于 2013-08-01T13:30:11.747 回答
0

如果要在检索到的日期小于当前日期时停用按钮,则应反转 if 语句条件,并使用date字符串而不是cmd

if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)

if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) >= 0)

因此,如果日期大于或等于当前日期,您可以将文件下载到服务器,如果日期小于当前日期,则禁用该按钮。

或者只是反转整个 if :

    if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) < 0)
    {
        Button6.Enabled = false;
    }
    else
    {
        DownLoadFileFromServer("~/NewFolder1/" + "Fundamentals of IS.pdf");
    }
于 2013-08-01T13:30:36.350 回答
0

您可能想要转换date

if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) < 0)

每次你点击那条线时都会抛出异常。

于 2013-08-01T13:31:13.590 回答
0

好吧,除了大家提到的明显问题外if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0) //need to use date instead of cmd,代码还包含一个反模式,它使用了过于通用的catch块。

该代码正在捕获一个异常来处理 IO 异常。但是如果还有其他类型的例外呢?我开始怀疑由于某种原因Convert.ToDateTime引发了一个错误,该错误被您认为仅捕获 IO 异常的 catch 语句所吞噬。事实上,它会捕获任何类型的异常,包括可能FormatException通过调用Convert.ToDateTime.

但这可能是SqlException我接下来要解释的:

此行中还有无效的 SQL:

MySqlCommand cmd = 
    new MySqlCommand("SELECT Date FROM fundamentals of is WHERE ChapNo=Chapter 1",
    connection);

我认为应该是:

MySqlCommand cmd = 
    new MySqlCommand("Select Date from fundamentals where ChapNo='Chapter 1'",
    connection); //that is, if ChapNo is a STRING/TEXT!

执行此命令将引发由于您的 catch 语句而无法检测到的错误。您需要使您的 catch 语句尽可能具体。

于 2013-08-01T14:06:48.980 回答
0

一种选择是在javascript中禁用它(在点击事件中)另一种选择是在Page_Onload中编写相同的代码(并检查ISPostback)

于 2013-08-01T13:40:08.960 回答
0
MySqlCommand cmd = new MySqlCommand("SELECT Date FROM fundamentals of is WHERE ChapNo=Chapter 1", connection);
string date = Convert.ToString(cmd.ExecuteScalar());
//date = cmd;
if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)

你确定要去Convert.ToDateTime(cmd)吗?

你不想Convert.ToDateTime(date)吗?

于 2013-08-01T13:29:51.600 回答
0

你有Button6.Enabled = false;一个 try catch 你没有throw an Exception。尝试将其复制到您的方法之上,如下所示,以查看它是否有效:

protected void Button6_Click1(object sender, EventArgs e)
{
    Button6.Enabled = false;
    MySqlConnection connection = new MySqlConnection("server=localhost;     ..
..
}

如果它有效,则在 try catch 内的语句中找到错误。

还要仔细检查你的aspx code. 也许您正在引用一个名为Button6_Click而不是发布的方法Button6_Click1(可能是 Button6_Click 的副本)

于 2015-05-30T15:38:18.460 回答