-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
  • 34,248
  • 6
  • 46
  • 76
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
  • 4,255
  • 1
  • 16
  • 26