Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sirs
I am self learning to C# I am doing a project for Assets management
I create user control showing two images (QRCode with BarCode) all ok ad these 2 images saved to sql DB by byte.but when I want to display them in the usercontrol only shows two copy of the image(QRcode) I know there is something wrong can I get help please and how to print the result my code :

What I have tried:

C#
public void LoadQRBRCode()
        {
            string qry = "Select * from tblQrBar  ";
            SqlCommand cmd = new SqlCommand(qry, MainClass.con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            foreach (DataRow item in dt.Rows)
            {
                Byte[] imagearray = (byte[])item["QrImage"];
                byte[] immagebytearray = imagearray;


                Byte[] imagearray1 = (byte[])item["barImage"];
                byte[] immagebytearray1 = imagearray1;

                AddItems("0", item["IDCode"].ToString(), Image.FromStream(new MemoryStream(imagearray)),
                    Image.FromStream(new MemoryStream(imagearray1)));
            }
Posted
Updated 22-Apr-24 11:27am
v2
Comments
PIEBALDconsult 22-Apr-24 17:29pm    
What does AddItems do?
Should the "0" be a different value on each call?

1 solution

There are a few problems here. With your existing code, you need to re-think it as the documentation[^] is very clear:
Remarks:

You must keep the stream open for the lifetime of the Image.
Since your stream is created and no reference to it is preserved once the method ends, it is liable for Disposal at any time the GC decides.

It's also considered a bad idea to use SELECT * FROM tblQrBar as it pulls all columns - you should specify only the data you are actually interested in to save bandwidth: SELECT QrImage, BarImage FROM tblQrBar

You also don't limit the number of rows you pull in, so if AddItem adds data to a control of some form then you are going to get a very slow and resource hungry app pretty quickly as codes are added to your system: Page them, sort them, search them, filter them - don't just dump them on the user and let him sort it out!

The problem you have noticed is likely to be outside that code: check your code to insert images to your DB - best guess is you INSERT the same image twice when you load your DB.
 
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