-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#

13
  • 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. Commented May 30, 2023 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: stackoverflow.com/a/66616751/10024425 Commented May 30, 2023 at 18:38
  • 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 Commented May 30, 2023 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. Commented May 30, 2023 at 19:36
  • 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. Commented May 30, 2023 at 21:21

1 Answer 1

-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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.