How to test for NULL in this scenario?

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

hi

asp.net 2.0

if no records exists in the database then an exception will be thrown in
GetPictureFromReader.
I want to perform a check on reader before calling GetPictureFromReader. so
only call GetPictureFromReader if records exists in reader..

how should I perform souch a test?

protected virtual List<PictureDetail>
GetPictureCollectionFromReader(IDataReader reader)
{
List<PictureDetail> pictures = new List<PictureDetail>();
while (reader.Read())
pictures.Add(GetPictureFromReader(reader));
return pictures;
}
 
You have to check on the columns:


Here is my typical IDataReader to business object code.



public override ICodeCollection SerializeCollection(System.Data.IDataReader
dr)

{

ICodeCollection returnCollection = new CodeCollection();

try

{

while (dr.Read())

{

ICode newItem = new Code();



if (!(dr.IsDBNull(0))) // assume the entity is good if the first column is
populated...<<ASSUMPTION

{





if (!(dr.IsDBNull(0)))

{

newItem.Property1 = dr.GetGuid(0);

}



if (!(dr.IsDBNull(1)))

{

newItem.Property2 = dr.GetInt32(1);

}

}



returnCollection.Add(newItem);

}

}

finally

{

}

return returnCollection;

}
 
Just check to see if the reader has any rows returned:

protected virtual List<PictureDetail>
GetPictureCollectionFromReader(IDataReader reader)
{
if(reader.HasRows)
{
List<PictureDetail> pictures = new List<PictureDetail>();
while (reader.Read())
pictures.Add(GetPictureFromReader(reader));
return pictures;
}
}
 
Back
Top