This is a migrated thread and some comments may be shown as answers.

gridview image datasource problem!!!

1 Answer 207 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Faheem Nadeem
Top achievements
Rank 1
Faheem Nadeem asked on 28 Aug 2009, 04:58 AM
Hi,
Recently i have faced a problem, which surely i think u guys will hae a solution.

Scenario:
I have a gridview, a picture box and a sql database.
Procedure:
Basically, its a client information entry form which takes in data from a user and stores it in a database and then recalls it in a gridview for administrative purposes.
The userclickes the picture box and gets a picture through a fileopendialog box, which is then converted into a byte array. i have made a client class which takes in all the client information using get/set properties, the image datatype is still unsure (need a little help on that too), then there is a stored procedure written which takes this class parameters and writes them to a database.
on the administrative side a connection is made to the sql server and a reader method is used which returns all the objects in the database and generates a list based on the client class. the class is then fed directly to the gridview as a datasource.
Problems:
Firstly there are no columns defined in gridview, it just takes the returned class and populates on that basis. probelm is with the image column. can u please guide me on storing image from picture box to the database and then retreiving into the gridview.
do i need to define all columns before populating it, and/or add a image column in it. Urgent help is required please.
PictureBox image convertsto byte array.
Sql database table has a field named 'ClientImage" of datatype 'image'
SqlReader method retrieves an object only, so one has to convert back to image 
display this image with other info in gridview.
can u please help me on all these datatypes and how to show image in gridiew. it would be more helpful if i dont have to specify any columns in gridview, and directly populate from datasource.

Thanks in advance!!!!







1 Answer, 1 is accepted

Sort by
0
Robert
Top achievements
Rank 1
answered on 28 Aug 2009, 10:43 PM
Hi Faheem,

If you set your class up to return the image in a byte array, the RadGridView should automatically generate the column.
public byte[] ImageData 
    get 
    { 
         return _imageData; 
    } 

Here is an alternative way you could retrieve the byte information from the file. (PictureBox.Image.Save proved to be somewhat problematic for me)
Image tmpImage = Image.FromFile(openFileDialog1.FileName); 
MemoryStream imageStream = new MemoryStream(); 
tmpImage.Save(imageStream, ImageFormat.Jpeg); 
Byte[] imageData = imageStream.ToArray(); 

As for the database, I would store the data in a field marked as varbinary(MAX). The image type is going to be removed in future versions of SQL Server (http://msdn.microsoft.com/en-us/library/ms187993.aspx).

I hope this helps.

- Robert
Tags
GridView
Asked by
Faheem Nadeem
Top achievements
Rank 1
Answers by
Robert
Top achievements
Rank 1
Share this question
or