1

Beginner here, I'm trying to update the image and the image's name from a different form and it's not updating. Also it doesn't give any errors. Is there something wrong in the code?

Form 2. this is where i update

  private void btnStockEdit_Click_1(object sender, EventArgs e)
    {
        try
        {
            sqlCon.Open();
            string qry = "Update SMStocksTb Set SmStockImgName=@SmStockImgName,SmStockImage=@SmStockImage where SmStockId=@SmStockId";
            SqlCommand cmd = new SqlCommand(qry, sqlCon);
            cmd.Parameters.AddWithValue("@SmStockId", SmStockId);

            cmd.Parameters.AddWithValue("@SmStockImgName", txtUPImgName.Text);
            cmd.Parameters.AddWithValue("@SmStockImage", Savephoto());

            cmd.ExecuteNonQuery();
            sqlCon.Close();
            MessageBox.Show("Update  Successfully","Updated",MessageBoxButtons.OK,MessageBoxIcon.Information);

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

    }
    private byte[] Savephoto()
    {
        MemoryStream ms = new MemoryStream();
        pbxUpdateImg.Image.Save(ms, pbxUpdateImg.Image.RawFormat);
        return ms.GetBuffer();
    }  

**Form1 ** this is where i open form 2 after selecting a row in datagrid

        private void btnStockEdit_Click(object sender, EventArgs e)
    {



        SMStockUpdateForm cfrmStockUpdateForm = new SMStockUpdateForm();
        try
        {
            if (StockListDG.CurrentRow.Index != -1)
            {
             SmStockId = Convert.ToInt32(StockListDG.CurrentRow.Cells[0].Value.ToString());


                 cfrmStockUpdateForm.txtUPImgName.Text = StockListDG.CurrentRow.Cells[11].Value.ToString();
                byte[] ImageArray = (byte[])StockListDG.CurrentRow.Cells[12].Value;

                if (ImageArray.Length == 0)
                    cfrmStockUpdateForm.pbxUpdateImg.Image = DefaultImage;

                else
                {
                    ImageByteArray = ImageArray;
                    cfrmStockUpdateForm.pbxUpdateImg.Image = Image.FromStream(new MemoryStream(ImageArray));
                }

            }
        }
        catch (Exception ex)
        {

        }

        cfrmStockUpdateForm.ShowDialog(this);

        if (isWindowOpen == false)
        {
            this.ParentForm.Opacity = 100;
        }



    }
2
  • Is the SQL code relevant to your question? If so, how? Commented Jul 10, 2018 at 0:53
  • The reason I ask about the SQL code is because your question talks about updating an image on one form from another form, yet your "update" code is an SQL update. It's not really clear what you're asking (to me at least). I've tried to help you by clarifying what you're asking, but if you're not willing to help us help you, then I'm out. Commented Jul 10, 2018 at 1:25

1 Answer 1

2

You should learn how to send data between two forms. From form1 you should only send the name of image and in form two should bind it according to its name you received from form1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.