Click here to Skip to main content
14,237,783 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello, i have this code

//the part for getting the byte array of image
MemoryStream image=new MemoryStream() ;
Bitmap  imgO = new Bitmap(open.FileName);
imgO.Save(image, ImageFormat.Bmp);

//the fill db part
           String sSQLCommand = "INSERT INTO ImageTable (CodImage,NameImage,Image,Distance,Split,Energy,Contrast,Entropy,Omogenity,Variance)" + "VALUES('"+Bob+"', '" + name + "','" + image.ToArray() + "','5','1','" + ai.Get_energy() + "','" + ai.Get_contrast() + "','" + ai.Get_entropy() + "','" + ai.Get_omogenity() + "','" + ai.Get_variance() + "' );";

           SqlCommand sqlc = new SqlCommand(sSQLCommand, sqlconn);

//and for view part i have this(in this part i think i have an error)
DataTable dt = new DataTable();
string sqlSelect = "select * from ImageTable";
SqlCommand cmd = new SqlCommand(sqlSelect, sqlconn);
  using (cmd)
               SqlDataAdapter adp = new SqlDataAdapter(cmd);

dataGridView1.DataSource = dt;

//sqlconn its the connection string;

i think its missing something and i cant find what, in ImageTable (the table of database) i have a collumn that stores data of type image, i converted the image to bytearray and succesfully stored it into database but when i want to view the images from database in a datagridview it throws me an error (something like parameter not valid). Can someone help me figureout where i made the mistake or there is another way to do the same thing?
Updated 14-Mar-12 0:41am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Please see this link[^]

at which, solution to similar problem is given

But I have a suggestion. Instead of creating DataTable, SqlCommand, SqlDataAdapter manually, you can try Typed DataSet using the AddNewDataSource wizard of Visual Studio.

And this article
General purpose class to fill DataTable(s) from DataBase and to save DataTable(s) to DataBase using reflection[^]

may be helpful to easily read data from database and save data to database.
In the example given in this article, in the Categories table of Northwind database, there is Picture field storing the image. I think it is relevant to your question.
ProEnggSoft 17-Mar-12 3:07am
Thank you
Rate this:
Please Sign up or sign in to vote.

Solution 2

String InsertImgTableCommand = "INSERT INTO ImageTable (CodImage,NameImage,Image,Distance,Split,Energy,Contrast,Entropy,Omogenity,Variance) VALUES(@CodImage, @NameImage,@Image,@Distance,@Split,@Energy,@Contrast,@Entropy,@Omogenity,@Variance )";
           SqlCommand sqlc = new SqlCommand(InsertImgTableCommand, sqlconn);
           sqlc.Parameters.AddWithValue("@CodImage", codimg.ToString());
           sqlc.Parameters.AddWithValue("@NumeImage", name);
           sqlc.Parameters.AddWithValue("@Image", image.ToArray());
           sqlc.Parameters.AddWithValue("@Distance", distance);
           sqlc.Parameters.AddWithValue("@Split", 1);
           sqlc.Parameters.AddWithValue("@Energy", ai.Get_energy());
           sqlc.Parameters.AddWithValue("@Contrast", ai.Get_contrast());
           sqlc.Parameters.AddWithValue("@Entropy", ai.Get_entropy());
           sqlc.Parameters.AddWithValue("@Omogenity", ai.Get_omogenity());
           sqlc.Parameters.AddWithValue("@Variance", ai.Get_variance());

           catch (Exception ex)


I managed to find the error and its in the insert command. And is should`ve been like this

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100