0

Basically, I just want to ask how to check a datatable value if it is null or not? I am looping through each cells on the table, and check if the value of a cell is null.

I tried this

If Not dt.Rows(i)(j).value Is Nothing Then
   MsgBox("cell empty")
End If

and

If Not dt.Rows(i)(j).value = "" Then
   MsgBox("cell empty")
End If

but it is not working.

As of now, my datatable looks like this

"e" means empty

"ne" means not empty

"x" dont care


Col1 Col2 Col3 ... 

ne   x    x   ...

x    e    x   ...

x    x    x   ...

examples from the table above..

dt(0)(0) = ne

dt(1)(1) = e

dt(0)(1) = either "e" or "ne"

PS, all the data in the datatable are all strings..

how can I get the value if dt(1)(1) is empty? C# codes would be okay too..thanks

EDIT:

somehow, I've managed to solve my question by doing this, it met my requirement in my program so.. yea.

If dt.Rows(i)(j).Value.ToString = "" Then
   MsgBox("empty")
End If

thanks. Please close the thread.

Codemunkeee
  • 1,585
  • 5
  • 17
  • 29
  • [Best way to check if a Data Table has a null value in it](http://stackoverflow.com/a/4604459/3089494) – pravprab Mar 12 '14 at 03:40
  • If you have found the answer, post it as a separate answer and accept it! It would help in maintaining the essence of StackOverflow. – Kashish Arora Mar 12 '14 at 04:14
  • I think you can close yourself question via vote for close? or you accept the one suggest you to do that? – V-SHY Mar 12 '14 at 04:16

4 Answers4

3

Compare the value to DBNull.Value.

foreach(DataRow row in myTable.Rows)
{
    object value = row["column"];
    if (value == DBNull.Value)
        // do something
    else
        // do something else
}

This was also answered on a different question here.

Community
  • 1
  • 1
Cory
  • 658
  • 3
  • 7
  • 19
2

http://msdn.microsoft.com/en-us/library/system.convert.isdbnull(v=vs.110).aspx

you can use IsDBNull method

while (dr.Read())
{
  dr.GetValues(fieldValues);

  for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++)
  {
     if (Convert.IsDBNull(fieldValues[fieldCounter]))
        fieldValues[fieldCounter] = "NA";
  }
  grid.Rows.Add(fieldValues);


 }
Lee Gary
  • 2,357
  • 2
  • 22
  • 38
1

You can try following

if(string.IsNullOrEmpty((string)dt.Rows[i][j].value))
{
  MsgBox("cell empty");
}
else
{
 MsgBox("cell is not empty")
}
Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25
1

For VB.net

I found this, sorry I'm not sure what to check inside the IsDbNull (if wrong please forgive me) because I have no experience deal with database in vb.net.

If wrong, please comment me to fix it, thanks.

If NOT IsDbNull(dt.Rows(i)(j)) Then
   MsgBox("cell non-empty")
ELSE
   MsgBox("cell empty")
End If

OR check much more than only DbNull like accepted answer in this post

If IsDBNull(dt.Rows(i)(j)) OrElse dt.Rows(i)(j).Value.ToString Is Nothing OrElse dt.Rows(i)(j).Value.ToString = string.Empty Then
   MsgBox("cell empty")
  1. If I want to check IsDBNull(), I should put dt.Rows(i)(j) or dt.Rows(i)(j).Value to check?
  2. If I want to check Is Nothing, I should put dt.Rows(i)(j) or dt.Rows(i)(j).Value or dt.Rows(i)(j).Value.ToStringto check?

thanks. Sorry for ask questions in question....

Community
  • 1
  • 1
V-SHY
  • 3,925
  • 4
  • 31
  • 47