如果检索到的日期小于当前日期,我想停用 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)
    {

    }
}

最佳答案

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

关于c# - 如何禁用按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17995366/

10-10 14:14