Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Frds,
In My Project I'm Using a datagridview with 32 columns of Text Box and 1 column as image column . my question is how to insert the datagridview image column to database (SQL Server 2008). I've Written same codings below but . It Doesn't works . Showing Error Message.

My Coding Is :
MemoryStream Stream = new MemoryStream();
string filename=datagridview1.cells[33].value;
Bitmaps image=new Bitmaps(filename);
image.Save(Stream,System.Drawing.Imaging.ImageFormat.Jpeg)
byte[] pic=new byte(image);


can AnyOne Help Me with Sample Coding....
Posted
Updated 30-Apr-13 6:24am
v3
Comments
Pheonyx 30-Apr-13 11:41am    
What error message? How are you trying to save it? What is image?
KalaiPondy 30-Apr-13 12:04pm    
I'm Trying to save the image as Image datatype in sql server ?
Error MSG: Invalid Column Range. (Shows at 2nd Line Of the Coding)
Mike Meinz 30-Apr-13 12:00pm    
You haven't told us what doesn't work. You say the problem is when you insert the image into the database but your code is saving an image to the hard drive not to the database. Please provide a complete (small) example that demonstrates the error, indicate the line in the code that throws the error and state the exact error message received. With good and concise information, we can provide quick and accurate help.
KalaiPondy 30-Apr-13 12:04pm    
I'm Trying to save the image as Image datatype in sql server ?
Error MSG: Invalid Column Range. (Shows at 2nd Line Of the Coding)
Mike Meinz 30-Apr-13 12:18pm    
That error message tells you that there are not 34 cells in datagridview1. Collections are indexed starting at 0. However, it is not clear from your example the class of datagridview1 because the .NET Framework DataGridView does not have a cells property. Again, a complete (small) example will get you answers quicker.

If datagridview1 is of type DataGridViewRow, then it will have a .Cells property and the maximum index that can be used to index the .Cells property will be the .ColumnCount property of the owner DataGridView Class minus 1.

1 solution

Change 33 to 31 and give that ago.

Sorry mean change it to 32 not 31.

The reason for this is that indexes in c# start at 0 and not at 1, so in order access column 33 you need to pass index 32.
 
Share this answer
 
v3
Comments
KalaiPondy 30-Apr-13 12:22pm    
I've Little Dout About filename . whether it gets the string FileName as bitmaps...
Pheonyx 30-Apr-13 12:26pm    
I'm sorry, I do not understand that? could you explain that further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900