0

I'm constant receive the error:

Additional information: Data is Null. This method or property cannot be called on Null values.

When I try to receive information. How can I check if the datareader is empty.

for example this is my code:

 while (rd.Read())
            {
                if (rd.HasRows)
                {
                    foundInformation[0] = rd.GetString(0);
                    foundInformation[1] = rd.GetString(1);
                }
                else
                {
                    foundInformation[0] = "nvt";
                    foundInformation[1] = "nvt";
                }               
            }

How can I check if rd.GetString(0) is empty?

Thankyou.

Jenssen
  • 1,801
  • 4
  • 38
  • 72

1 Answers1

9

You need to call IsDBNull before you call GetString.

if (!rd.IsDBNull(0))
{
  //...
}

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.isdbnull%28v=vs.110%29.aspx

William Xifaras
  • 5,212
  • 2
  • 19
  • 21