简体   繁体   中英

The Image in the picture box does not change

Please I need help here. I am using data from a gridview to fill textboxes and a pictureBox. The textboxes fill up just fine when I click on a row. But the picture box shows just one image. irrestpective of which row I select the same image shows up. Please I am new to c# and I need help. Here is what I have done so far private void viewMemberGrid_CellClick(object sender, DataGridViewCellEventArgs e) {

        DataGridViewRow dr = viewMemberGrid.SelectedRows[0];
        GmemIDBox.Text = dr.Cells[0].Value.ToString();

        GsurnameBox.Text = dr.Cells[1].Value.ToString();
        GfirstnameBox.Text = dr.Cells[2].Value.ToString();
        GphoneBox.Text = dr.Cells[3].Value.ToString();
        memDeptBox.Text = dr.Cells[7].Value.ToString();
        memPortBox.Text = dr.Cells[8].Value.ToString();
        GaddBox.Text = dr.Cells[5].Value.ToString();
        GdobBox.Text = dr.Cells[6].Value.ToString();

        string select = "Select NEWMEMBER.MemberID, Surname, Firstname, PhoneNumber, MemberPic, HomeAddress,DOB,DeptID,PortfolioDesc from NEWMEMBER inner join Pictures on NEWMEMBER.MemberID = Pictures.MemberID join PortfolioDescription on PortfolioDescription.PortfolioId = NEWMEMBER.PortfolioID";
        SqlConnection c = new SqlConnection("Data Source=.;Initial Catalog=Cmanager;Integrated Security=True");
        SqlCommand cmd = new SqlCommand(select, c);

        try
        {
            c.Open();
            SqlDataReader dro = cmd.ExecuteReader();

            if (dro.Read())
            {
                byte[] picarr = (byte[])dro["MemberPic"];
                MemoryStream ms = new MemoryStream(picarr);
                ms.Seek(0, SeekOrigin.Begin);
                memPicBox.Image = Image.FromStream(ms);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        c.Close();

    } 

Make a little change to your select statement

;

I dont know whether your MemberID Column in the database is a Number or Varchar

.

;

If it's a Number

string select = "Select NEWMEMBER.MemberID, Surname, Firstname, PhoneNumber, MemberPic, HomeAddress,DOB,DeptID,PortfolioDesc from NEWMEMBER inner join Pictures on NEWMEMBER.MemberID = Pictures.MemberID join PortfolioDescription on PortfolioDescription.PortfolioId = NEWMEMBER.PortfolioID Where NEWMEMBER.MemberID = " + dr.Cells[0].Value.ToString();

Or if it's Varchar

string select = "Select NEWMEMBER.MemberID, Surname, Firstname, PhoneNumber, MemberPic, HomeAddress,DOB,DeptID,PortfolioDesc from NEWMEMBER inner join Pictures on NEWMEMBER.MemberID = Pictures.MemberID join PortfolioDescription on PortfolioDescription.PortfolioId = NEWMEMBER.PortfolioID Where NEWMEMBER.MemberID = '" + dr.Cells[0].Value.ToString() + "'";        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM