0

I have inserted an image on the 1st form namely Add_Staff and want to get that image on the 2nd form namely Staff_Detail's data gridview. how I can pass reference of add_staff images to staff_detail form's data gridview. Here is the code.

Insertion Code: -

    private void BTNSTAFF_Click(object sender, EventArgs e)
    {

            if (staffid.Text == "")
            {

                if (teachername.Text == "" || saddress.Text == "" || semail.Text == "" || contact.Text == "" || jobspeciality.Text == "")
                {
                    MessageBox.Show("All Fields Required");
                }
                else
                {

                    Image pimg = pictureBox1.Image;
                    ImageConverter converter = new ImageConverter();
                    var ImageConvert = converter.ConvertTo(pimg, typeof(byte[]));

                    conn.Open();

                    //Values Inserted into Course

                    SqlCommand cmd = new SqlCommand("insert into staff values (@a,@b,@c,@d,@e,@g)", conn);
                    cmd.Parameters.AddWithValue("@a", teachername.Text);
                    cmd.Parameters.AddWithValue("@b", saddress.Text);
                    cmd.Parameters.AddWithValue("@c", semail.Text);
                    cmd.Parameters.AddWithValue("@d", contact.Text);
                    cmd.Parameters.AddWithValue("@e", jobspeciality.Text);
                    cmd.Parameters.AddWithValue("@g", ImageConvert);

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Data Inserted");

                    frm1.RefreshGrid();

                    conn.Close();

                    Staff_Clear();

                    this.Hide();


                }
            }}

Staff Detail Code for View Deatil: -

public partial class Staff_Detail : Form
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mudas\source\repos\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1\Database1.mdf;Integrated Security=True");


    public static string column_id = "";
    public static string column_name = "";
    public static string column_address = "";
    public static string column_email = "";
    public static string column_contact = "";
    public static string column_job = "";
    public Staff_Detail()
    {
        InitializeComponent();
        View();
    }

    public void View()
    {

        try
        {


            dataGridView4.Rows.Clear();

            // if (conn.State != ConnectionState.Open)
            conn.Open();

            SqlCommand cmd = new SqlCommand("Select * From staff", conn);
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                String column_getid = dr["id"].ToString();
                String column_getname = dr["name"].ToString();
                String column_getaddress = dr["address"].ToString();
                String column_getemail = dr["email"].ToString();
                String column_getcontact = dr["contact"].ToString();
                String column_getjob = dr["job"].ToString();

                dataGridView4.Rows.Add(column_getid, column_getname, column_getaddress, column_getemail, column_getcontact, column_getjob, "Edit/Delet");


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


    private void dataGridView4_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

        int rowIndex = dataGridView4.CurrentCell.RowIndex;

        String Column_id = dataGridView4.Rows[rowIndex].Cells[0].Value.ToString();
        String Column_name = dataGridView4.Rows[rowIndex].Cells[1].Value.ToString();
        String Column_address = dataGridView4.Rows[rowIndex].Cells[2].Value.ToString();
        String Column_email = dataGridView4.Rows[rowIndex].Cells[3].Value.ToString();
        String Column_contact = dataGridView4.Rows[rowIndex].Cells[4].Value.ToString();
        String Column_job = dataGridView4.Rows[rowIndex].Cells[5].Value.ToString();

        column_id = Column_id;
        column_name = Column_name;
        column_address = Column_address;
        column_email = Column_email;
        column_contact = Column_contact;
        column_job = Column_job;

        Add_Staff ad = new Add_Staff(this);
        ad.Show();
        ad.BringToFront();

    }
}
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 1
    Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – JohnG Aug 11 '21 at 10:44
  • If I'm reading this right, you're inserting the image into the database. So in the detail form you just retrieve the image form the database and convert it back to an image? – stuartd Aug 11 '21 at 10:45
  • @JohnG the link you mentioned I don't understand. It's quite different. – Mudasir Rasheed Aug 11 '21 at 10:53
  • The link shows you how to pass the `Image` or just about anything from 1 form to a different form. As stuartd's comment notes, since it “appears” the code is saving the image to the DB, you can avoid this altogether by getting the image from the DB in form 2. – JohnG Aug 11 '21 at 10:57
  • Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/) • [Interaction between forms - How to change a control of a form from another form?](https://stackoverflow.com/questions/38768737/) • [Passing data between forms](https://stackoverflow.com/questions/4587952/) • [Passing variable between winforms](https://stackoverflow.com/questions/4247807/) • [Passing data between C# forms](https://stackoverflow.com/questions/39188704/) –  Aug 11 '21 at 11:03
  • I will check these link – Mudasir Rasheed Aug 11 '21 at 11:16

0 Answers0