Search code examples
c#datagridviewoledbrows

Check DataGridView for Nulls


I am checking my datagridview in a specific column for a null value, but i cant seem to get it working.

Here is my code : Thanks.

        for (int i = 0; i < (DataGridView1.Rows.Count - 1); i++)
      {
          string colTimeOut = DataGridView1.Rows[i].Cells[4].Value.ToString();    
          MessageBox.Show(colTimeOut);
          if (DataGridView1.Rows[i].Cells[4].Value == null || 
          DataGridView1.Rows[i].Cells[4].Value == string.Empty ||
          DataGridView1.Rows[i].Cells[4].Value == "")

          {
              OLEDB_Connection.Open();
              updateCmd.Connection = OLEDB_Connection;
              updateCmd.CommandText = "INSERT INTO TestDB (TimeOut) VALUES (@TIMEOUT)";
              updateCmd.Parameters.AddWithValue("@TIMEOUT", varTime);
              updateCmd.ExecuteNonQuery();
              OLEDB_Connection.Close();

          }

      else  

Solution

  • try this

      for (int i = 0; i < (DataGridView1.Rows.Count); i++)
      {
          string colTimeOut = DataGridView1.Rows[i].Cells[4].Value.ToString();    
          MessageBox.Show(colTimeOut);
          if (String.IsNullOrEmpty(colTimeOut))
    
          {
              OLEDB_Connection.Open();
              updateCmd.Connection = OLEDB_Connection;
              updateCmd.CommandText = "INSERT INTO TestDB (TimeOut) VALUES (@TIMEOUT)";
              updateCmd.Parameters.AddWithValue("@TIMEOUT", varTime);
              updateCmd.ExecuteNonQuery();
              OLEDB_Connection.Close();
    
          }
    
      else