Home > Mobile >  Mysql get value
Mysql get value

Time:07-30

Since yesterday I am trying to get this value (see image), I have searched a lot on google, and I have tried to use "mysqlreader, executescalar and more", but I cannot get the number of lines.

What I want to do is this If the result is 0 it does nothing, if equal to 1 it must show an image, if greater than 1 it must show another image

phpmyadmin count

ex code 1

        private void patient()
        {
            if (OpenEventMissionData.Rows.Count != 0)
            {
                foreach (DataGridViewRow row in OpenEventMissionData.Rows)
                {
                    string idevent = row.Cells[1].Value.ToString();
                    string sql = "SELECT COUNT(*) FROM patient INNER JOIN event WHERE patient.ID_EVENT = "   "'"   idevent   "'"   "AND evento.EVENT_OPEN = 1;";

                    MySqlConnection connection = new MySqlConnection();
                    connection.ConnectionString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
                    MySqlCommand cmd = new MySqlCommand(sql, connection);
                    connection.Open();
                    MySqlDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {   
                        DataGridViewButtonColumn patient = new DataGridViewButtonColumn();
                        OpenEventMissionData.Columns.Add(new PatientColumn());
                    }
                }
            }
        }

EDIT:

(sorry if I don't translate the code into English, I'm keeping only this part of the code in another language until I solve it), I tried adding the code that told me @oldDog, but the result is always 6

ex code 3

NEW EDIT:

in fact 6 lines appear, what the fuck

phpmyadmin

CodePudding user response:

Your problem here is you are using HasRows. Since you are doing SELECT COUNT(*) you will always have one row which will contain the count, even if the count is zero. Instead you could use:

if (Convert.ToInt32(reader[0]) > 0)
  • Related