error handling

  • Thread starter Thread starter jack
  • Start date Start date
J

jack

hi all
im facing a problem in error handelling.
what im doing is getting the images from the database to my
csharp application form .. now here in some of the records contains
images along with the data where as some as has null value ..
who to trap this please help
thanks for the help..
 
jack said:
im facing a problem in error handelling.
what im doing is getting the images from the database to my
csharp application form .. now here in some of the records contains
images along with the data where as some as has null value ..
who to trap this please help

Well, firstly - how are you retrieving the data? Whichever way it is,
there's bound to be something like IsDbNull() for the appropriate
column. (Alternatively, you can get the value and test for it being
DbNull - with "if (value is DbNull)".

You then need to work out what to do with those records, of course.

Jon
 
Hi,

You have to check for null , if so you have to decide what to do, either
assign null to the variable or just use some predefined image.
Here is some code, it was extracted from a bigger program so it does not
compile, it's just for you to look into it


protected override void Load()
{
//generate a new local name for this file
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "LoadDocument";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dID", SqlDbType.Int).Value = id;
SqlDataReader reader = DataProvider.ExecuteReader( cmd);
while( reader.Read() )
{
this.comment = "";
this.origname = reader["OrigName"].ToString();
this.dDate = Convert.ToDateTime( reader["dDate"]);

this.name = Guid.NewGuid().ToString() + "." + origname.Substring(
origname.LastIndexOf(".")+1);
physicalname = physicalname + @"\" + name;
ExtracFileFromDB( reader);

}
reader.Close();
}
protected void ExtracFileFromDB( SqlDataReader reader)
{
if ( reader["Data"] != DBNull.Value )
{
FileStream file = new FileStream( physicalname, FileMode.Create);
file.Write( (byte[])reader["Data"], 0,
((byte[])reader["Data"]).GetUpperBound(0)+1 );
file.Close();
}

}



cheers,
 
Back
Top