-3

This is the code I use to add the values into the database:

SqlConnection con = new SqlConnection(@"Data Source=WIN-39OFKTSHQUA;Persist Security Info=True;User ID=sa;Password=asd123");

con.Open();

int id = Convert.ToInt16(textBox1.Text);
string name = textBox2.Text;
string fname = textBox3.Text;
string conc = textBox4.Text;
string mail = textBox5.Text;
string add = textBox6.Text;
int age = Convert.ToInt32 (textBox7.Text);

MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, ImageFormat.Jpeg);
Byte[] imageArray = stream.ToArray();

SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Student values ('" + id + "','" + name + "','"+age+"','" + fname + "','" + conc + "','" + mail + "','" + add + "','"+imageArray+"')";

cmd.ExecuteNonQuery();

Now, I want to retrieve the image on another form into a picturebox. I have applied all the solutions that are available on the internet but I am unable to retrieve the image and nothing is working, every time I get an error and most of the times the error is "Parameter is not valid".

For now, I am retrieving data of the selected ids from combobox except for image, please help me.

I tried many solutions some of the recent ones are following

Loading PictureBox Image From Database

https://www.codeproject.com/Tips/465950/Why-do-I-get-a-Parameter-is-not-valid-exception-wh

Displaying image from db to picturebox winforms c#

Thom A
  • 88,727
  • 11
  • 45
  • 75
  • What is the Type of the Field that contains the images? It should be `VARBINARY(MAX)`. So you just retrieve the byte array from there -- You need to specify the Type of all the Columns in your database. You're trying to insert everything (ID - hoping it's not the automatic indexer - and the bytes of the image included) as strings now -- That's not the way to build a query. Use Parameters instead -- You should also show the code that tries to retrieve the image bytes – Jimi May 30 '23 at 18:36
  • 1
    Converting a `byte[]` array to a string will yield something like "System.Byte[]". You should anyhow use a parametrized statement instead of building an SQL statement by string concatenation. – Klaus Gütter May 30 '23 at 18:37
  • 1
    _i have applied all the solutions that are available on the internet but i am unable to retrieve the image and nothing is working_: The internet is a vast place, Your statement seems to be an exaggeration, a fallacy, or perhaps even a lie. The following may be of interest: https://stackoverflow.com/a/66616751/10024425 – Tu deschizi eu inchid May 30 '23 at 18:38
  • @Tudeschizieuinchid you are right about my statement its just that I'm frustrated right now and nothing seems to be working that's why I wrote it. can any of you please tell me how to write a parameterized query cause I know very little about sql queries – Hamza Siddiqui May 30 '23 at 19:06
  • @Jimi my id is not set to automatic and the stdImage has Image type in db. – Hamza Siddiqui May 30 '23 at 19:11
  • 2
    The `Image` Type is deprecated. As mentioned, use `VARBINARY(MAX)` -- The code you presented here, sorry to say it, is wrong in all departments (and cannot work, see my first comment). Also, you REALLY need to use Parameters, don't concatenate strings to build a Query, it's very important – Jimi May 30 '23 at 19:26
  • 1
    There are THOUSANDS of posts here showing how to use parameterized queries. That is of course in addition to the online documentation. It is worth noting that one of your links already shows it. – Ňɏssa Pøngjǣrdenlarp May 30 '23 at 19:36
  • @Jimi I've changed it to varbinary (max) and now I'm writing a parameterized query, I will tell you if the issue gets resolve. – Hamza Siddiqui May 30 '23 at 19:38
  • The post that I mentioned in my previous comment will solve your issue, including how to write a parameterized query. I highly recommend that you look at it, as well as the comments within the code. – Tu deschizi eu inchid May 30 '23 at 19:38
  • @ŇɏssaPøngjǣrdenlarp I'm doing it right now, I was just getting paranoid by thinking what if it is right or wrong. – Hamza Siddiqui May 30 '23 at 19:39
  • thank you very much to all of you, after writing parameterized query it is working fine, again thank you very much you. – Hamza Siddiqui May 30 '23 at 19:58
  • 2
    Please do not add "solved" to your question (title). If someone has answered your question with a solution that works for you then accept the answer as the solution and/or upvote it. – Thom A May 30 '23 at 21:21
  • @ThomA i got my answer from comments so there isn't an option to mark it, anyway i have posted the comment that worked for me as an answer and will tick it tomorrrow after the time limit. – Hamza Siddiqui May 31 '23 at 10:19

1 Answers1

-2

this answer was given by Jimi...

What is the Type of the Field that contains the images? It should be VARBINARY(MAX). So you just retrieve the byte array from there -- You need to specify the Type of all the Columns in your database. You're trying to insert everything (ID - hoping it's not the automatic indexer - and the bytes of the image included) as strings now -- That's not the way to build a query. Use Parameters instead -- You should also show the code that tries to retrieve the image bytes

Thom A
  • 88,727
  • 11
  • 45
  • 75