Home  >  Q&A  >  body text

Get the value of MySQL

Since yesterday, I'm trying to get this value (see picture), I've tried using "mysqlreader, executescalar, etc." but I can't get the number of rows.

What I want to do is this:

If the result is 0, do nothing; if equal to 1, an image must be displayed; if greater than 1, another image must be displayed

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());
            }
        }
    }
}

I tried adding the code that told me @oldDog but the result is always 6

New editor:

actually 6 lines appear.

P粉323374878P粉323374878154 days ago358

reply all(1)I'll reply

  • P粉668146636

    P粉6681466362024-04-07 12:44:55

    Your problem is that you are using HasRows. Since you are doing a SELECT COUNT(*), you will always have a row containing the count, even if the count is zero. Instead, you can use:

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

    reply
    0
  • Cancelreply