How to disable the button

I want to deactivate button6 if the received date is less than the current date, for this I used the code below, but it does not work. Please help me find a mistake.

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

This serverMapPath code

 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
source share
8 answers

MSDN answer:

I thought it would help you.

 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"; 
+1
source
 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) 

Are you sure you will go to Convert.ToDateTime(cmd) ?

Didn't you want Convert.ToDateTime(date) ?

0
source

The following line may have a logical error, as you convert the cmd variable to DateTime instead of date , which is returned by your command:

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

Using this instead can solve your problem:

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

If you want to deactivate the button when the received date is less than the current, you must invert the condition of the if statement and use the date string instead of cmd :

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

to

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

so that you can upload the file to the server if the date is greater than or equal to the current date, and disable the button if the date is less than the current.

Or just invert the integer if:

  if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) < 0) { Button6.Enabled = false; } else { DownLoadFileFromServer("~/NewFolder1/" + "Fundamentals of IS.pdf"); } 
0
source

You probably want to convert date :

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

it will throw an exception every time you click on this line.

0
source

One parameter disables it in javascript (in the click event); another option is to write the same code in Page_Onload (and check ISPostback)

0
source

Well, besides the obvious problem that everyone was talking about if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0) //need to use date instead of cmd , the code contains an anti-template that uses too general catch blocks.

The code captures the exception for handling IO exceptions. But what if there are other types of exceptions? I'm starting to suspect that for some reason Convert.ToDateTime throws an error that is swallowed by the catch statement, which, in your opinion, only catches IO exceptions. In fact, it will catch any exception, including a possible FormatException that may be FormatException by calling Convert.ToDateTime .

But it could be a SqlException , as I will explain below:

This line also has invalid SQL:

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

I think it should be:

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

executing this command will result in an error that you cannot detect due to your catch statement. You should make your withdrawal statements as specific as possible.

0
source

You have Button6.Enabled = false; inside try catch where you are not throw an Exception . Try copying it at the top of your method as shown below to see if it works:

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

if it works, then find the error in your statements inside try catch.

Also double check your aspx code . Perhaps you are referring to a method called Button6_Click , and not as sent by Button6_Click1 (perhaps Button6_Click)

0
source

Source: https://habr.com/ru/post/1494743/


All Articles