lundi 11 mai 2015

C# form display image and text on datagrid from sql

I Have a form with a database with images and text associated to each image Im trying to display all the data in the database on a form. I have a form with a picture box but when I call the stored data(using a button "View all" I only see one picture on my form. I thought perhaps a better way would be use a datagrid generated at run time to display "EID" "First Name" and "Last Name" as well as "Image" (the data stored in the database to allow for a proper display but am struggling to do so. How would I do it via datagridview to ensure that all my data are correctly displayed when the button "view all" is pressed. Apologies if some of this is unclear I am quite new to C#.Basically Im trying to display all database parameters for all entries on a datagrid

        try
        {
            string sql = "SELECT* FROM Employee";
            if (conn.State != ConnectionState.Open)
                conn.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand(sql,conn);
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                Console.WriteLine(myReader["EID"].ToString());
                Console.WriteLine(myReader["FIRST_NAME"].ToString());
                Console.WriteLine(myReader["LAST_NAME"].ToString());
                byte[] img = (byte[])(myReader["IMAGE"]);
                if (img == null)
                    pictureBox1.Image = null;
                else
                {
                    MemoryStream ms = new MemoryStream(img);
                    pictureBox1.Image = Image.FromStream(ms);
                }

            }


        }
        catch (Exception ex)
        {
            conn.Close();
            MessageBox.Show(ex.Message);
        }

Aucun commentaire:

Enregistrer un commentaire