skip to Main Content

I want to make visible a button, but only when a string exists. This is the way I thought, but I’m not seeing any change to my UI. Furthermore, the loop doesn’t exit, despite only returning a single result.

using (MySqlConnection con = new MySqlConnection(ConnStr))
{
    con.Open();
               
                
    string sqlCON = "SELECT CV_EXIST FROM DATA_NEURON.DM_CV WHERE DWH_CUSTOMER_KEY = '123321'";
    MySqlCommand cmd = new MySqlCommand(sqlCON con);
                
    using (MySqlDataReader rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())   
        {
            var check = rdr.GetString(0);
                        
            if (check == "YES")
            {
                button_cv.Visible = true;
            }
            else
            {
                button_cv.Visible = false;
            }
        }
    }
}

Thank you and sorry for my english.

2

Answers


  1. Please note, that in your current implementation button_cv doesn’t change its state when cursor is empty and while loop is not entered; another
    possible issues are (trailing) spaces ("YES ") and case ("yes", "Yes" etc.). If it is "doesn’t work" then I suggest something like this:

    using (MySqlConnection con = new MySqlConnection(ConnStr)) {
      con.Open();
    
      //DONE: please, keep SQL readable
      //DONE: parametrize the query or have a good reason not to do it  
      string sqlCON = 
         @"SELECT CV_EXIST  
             FROM DATA_NEURON.DM_CV 
            WHERE DWH_CUSTOMER_KEY = @Key";
    
      //DONE: do not forget to dispose IDisposable - using
      using (MySqlCommand cmd = new MySqlCommand(sqlCON, con)) {
        //TODO: you may want to change MySqlDbType type, to, say, MySqlDbType.Int32
        cmd.Parameters.Add("Key", "123321", MySqlDbType.VarString);
    
        // You don't need the entire cursor here, just the 1st record if it exists
        button_cv.Visible = "YES".Equals(
          cmd.ExecuteScalar()?.ToString()?.Trim(), 
          StringComparison.OrdinalIgnoreCase);
      }
    }
    

    Here I’ve assumed that empty cursor means NO; if it is YES then all you need is to change a single line by adding ?? "YES":

      cmd.ExecuteScalar()?.ToString()?.Trim() ?? "YES", 
    
    Login or Signup to reply.
  2. this code has syntax error :


    MySqlCommand cmd = new MySqlCommand(sqlCON con);
    

    best way is write a method for recognize button visibility and use its :


    public static bool GetButtonVisibility()
    {
        using (var con = new MySqlConnection("connection string"))
        {
            con.Open();
            var query = "SELECT CV_EXIST FROM DATA_NEURON.DM_CV WHERE DWH_CUSTOMER_KEY = '123321'";
            var cmd = new MySqlCommand(query, con);
            using (var rdr = cmd.ExecuteReader())
                return rdr.Read() && rdr.GetString(0) == "YES";
        }
    }
    

    button_cv.Visible = GetButtonVisibility();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search