+ 1

Why is my code not working??

Hello, I'm trying to load images from SQL SERVER into a picturebox in C# but it's not working 😭 I found this code online and it isn't working, what can I do! SqlDataAdapter dataAdapter = new SqlDataAdapter(new SqlCommand("SELECT pic FROM imageTest WHERE pic_id = 1", yourConnectionReference)); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); if (dataSet.Tables[0].Rows.Count == 1) { Byte[] data = new Byte[0]; data = (Byte[])(dataSet.Tables[0].Rows[0]["pic"]); MemoryStream mem = new MemoryStream(data); yourPictureBox.Image= Image.FromStream(mem); } https://www.sololearn.com/discuss/1798865/?ref=app

12th May 2019, 11:41 PM
Cybele Ghanem
Cybele Ghanem - avatar
6 Réponses
+ 1
Yes I have the database in sql and no there isn't an error, just the picture isn't showing up
13th May 2019, 7:09 PM
Cybele Ghanem
Cybele Ghanem - avatar
+ 1
Everything that u mentioned is fine in the code so that's why idk why it isn't working😭
13th May 2019, 7:10 PM
Cybele Ghanem
Cybele Ghanem - avatar
0
Add debug lines so you can see where it goes wrong. Is there a access violation that occurs or is the picture just not showing ? Start with adding a try catch try { //your code } catch(exception ex) { MessageBox.Show(ex.message); } Is the a exception that occurs and what does the message tell you ? Do you have a database in a sql server ? With a table named "imagetest" and a field "pic" and a field "pic_id" Does the field pic contains a image ? is your sql connection open ? do you do myConnection .Open(); Can you check the state of the sql connection ? if (myConnection != null && myConnection.State == ConnectionState.Closed) { } https://www.dotnetperls.com/sqlconnection
13th May 2019, 6:54 PM
sneeze
sneeze - avatar
0
Thank you. Good to know that it is working
13th May 2019, 7:11 PM
sneeze
sneeze - avatar
0
Start with adding mem.position = 0; or mem.Seek(0, SeekOrigin.Begin); to set the memorystream to start position. Check if the memory stream hold data, you cannot read the result but you must be able to see that there are bytes in there. Last thing, try to figure out the size of the picture according to that of the picture box, if the image is to big or to small it will not show.
13th May 2019, 7:22 PM
sneeze
sneeze - avatar
0
Okay thank you I'll try to do so and let u know if it didn't work
13th May 2019, 7:27 PM
Cybele Ghanem
Cybele Ghanem - avatar