Hello! Do not scold strictly. I have such a problem from mysql database pulling out a cell with a specific id. Like this.

public ArrayList checkOnFilling(string id) { ArrayList checkFiling = new ArrayList(); using (MySqlConnection myConnection = new MySqlConnection(myConnectionString)) { try { myConnection.Open(); string CommandText =String.Format("SELECT id_voprosGos FROM students where id = {0} ", Convert.ToInt32(id)); MySqlCommand myCommand = new MySqlCommand(CommandText, myConnection); MySqlDataReader MyDataReader; MyDataReader = myCommand.ExecuteReader(); if (MyDataReader.HasRows) foreach (DbDataRecord result in MyDataReader) checkFiling.Add(result); MyDataReader.Close(); return checkFiling; } catch (System.Exception ex) { //MessageBox.Show(ex.Message); return null; } finally { myConnection.Close(); } } } 

I perform the check as follows.

  private void FormFolder_Load(object sender, EventArgs e) { this.Text = Program.namePath; dl.createFolserStudent(Program.namePath); ArrayList checkOnFilling = dl.checkOnFilling(Program.id); if(checkOnFilling == null) { MessageBox.Show("У студента + " + Program.namePath + " не создан первый прокол "); radioButton2.Enabled = false; } else { MessageBox.Show("У студента + " + Program.namePath + " уже создан первый потакол "); radioButton2.Enabled = true; } } 

But my query still returns one record, regardless of whether the cell is empty or not. enter image description here

  • one
    You re-read your question. In the first block of code, the list of allStudent students is being filled, and with checking for MyDataReader.HasRows , i.e. without data, the list will remain empty. But in the second block nothing is clear what dl.checkOnFilling(Program.id); ? Offer wangovat? - Bulson
  • I pass the id - it also gets into the database query - Leonid Smirnov
  • Fully support @Bulson. - Alexander Muksimov
  • dl is an object of a class, and what comes after the dot is how it is called with the request. - Leonid Smirnov
  • one
    This is charming, but for now it can be seen that a certain dl has two methods: createFolserStudent () and checkOnFilling (), but what such a dl is is not clear. - Bulson

2 answers 2

 if(MyDataReader["id_voprosGos"] == System.DBNull.Value) 

The guys from the chat suggested that you need to think in the direction of DbNull . Solution above! Thank you all.

    Personally, I prefer to work with a micro-orm database. For example Peta-Poco.

    So you can quickly and sufficiently work with databases of any size, as well as work with records in the database as with objects.

    that is, to get the desired object by ID, for example:

     var article = db.Single<Article>(123); 

    and then any article article field is checked for null. That's all.

    If you work with ORM instead of micro-ORM, even such a request will not need to be done. Just a LONQ request for a search by ID and exactly the same check of the desired parameter.

    ORMKI - it is very convenient.

    I advise.