How to test for NULL in this scenario?

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;
}
 
S

sloan

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;

}
 
S

Scott M.

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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top