Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
FOR PICTURE SHOWING in picture box again employee id
WHAT WILL I DO.
FOR EXAMPAL ( textBox1.Text = dr.GetString(1)
pictureBox1.image = ???

My Code is below
C#
bool temp = false;
SqlConnection con = new SqlConnection("server=WASEEM\\SQLEXPRESS;database=malik;Trusted_Connection=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Table2 where first='" + textBox1.Text.Trim() + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textBox2.Text = dr.GetString(1);
textBox3.Text = dr.GetString(2);
pictureBox1.Image = dr.GetString(3);
temp = true;
}
if (temp == false)
MessageBox.Show("not found");
con.Close();
}
}


What I have tried:

Picture show automaticlly in picture box again employee ID
Posted
Updated 17-Aug-16 19:58pm

1 solution

It's going to depend on exactly what you stored in your DB as the Image: you are retrieving it as a string, so you could have stored the URL of the image, the image as a base64 string, or even just tried to dump the Image class instance into the DB.
We don't know.

What you need to do is start by looking at how you store it to find out exactly what you store, and then reverse the process to get an Image control instance to set into the PictureBox.

But...the most common mistake is to save the Image via myImage.ToString(), or convert it to a byte array, and sat that with myImageData.ToString() - neither of which will work and both of which will mean your DB contains useless rubbish. Have a look here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows one way to do it properly.

And do yourself several favours:
1) Stop using SELECT * and name the fields you want to fetch:
SQL
SELECT ID, UserName, UserImage FROM ...
It's a lot more efficient.
2) Don't use numeric indexes into the DataReader: named indexes make your code easier to read and much more reliable.
3) Stop using default names for everything: Table2, textBox3 - you may remember now what they contain, but you won't in three weeks when you need to change the code. use "proper" names which describe what they do: tabUsers, tbUserName, ... they make your code more self documenting, and that improves reliability as well.
4) And really, really important: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 

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