Displaying images from a database

S

SamIAm

Hi

I have a form and need to display an image that is returned along with other
data, i.e. Member Data, from a database row.
How do I display this image in my webform?

S
 
I

Ignacio Machin

Hi Sam,

You need to load the image from the DB, create a file accesible from the
IIS server and then set the ImageUrl property of the image to this new file,
here is the code I'm using:

//First load the image fromt he DB:
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "LoadDocument";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@dID", SqlDbType.Int).Value = id;

SqlDataReader reader = DataProvider.ExecuteReader( cmd);



//Now save the file, see that I use a Guid for name the file, as I put all
the files in the same folder it can be possible that two images had the same
name originally.

//This line is to keep a reference of the new name, NOTE that as I use
different kind of files ( not only images) I have to keep the extension of
the file

name = Guid.NewGuid().ToString() + "." + origname.Substring(
origname.LastIndexOf(".")+1);

//Physicalname has the path where the file will be saved

physicalname = physicalname + @"\" + name;

FileStream file = new FileStream( physicalname, FileMode.Create);

file.Write( (byte[])reader["Data"], 0,
((byte[])reader["Data"]).GetUpperBound(0)+1 );

file.Close();



Hope this help,
 

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