-1

I am using the following code for read all image paths that stored in img column in tb21 table.

NpgsqlDataAdapter da = new NpgsqlDataAdapter("SELECT img FROM tb21", dbcon);
DataSet ds = new DataSet();
List<string> img1 = new List<string>();

foreach (DataRow row in ds.Tables["tb21"].Rows)
{ 
    img1.Add(row["img"].ToString()); 
}

But, I got an error as:

object reference not set to an instance of an object

what is solution for this?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Kurd
  • 11
  • 6

1 Answers1

1
if (ds.Tables.Count > 0)
  foreach (DataRow row in ds.Tables["tb21"].Rows)
    if(row["img"] != null && row["img"] != DBNull.Value)
       { img1.Add(row["img"].ToString()); }
mkysoft
  • 5,392
  • 1
  • 21
  • 30