I'm trying for several hours to figure it out how can I refresh a previous form (Form1) when I save data on Form2.
I'm using btnAdd_Click
on Form1 in order to open me Form2 where user can enter data, so far so good. What I want is: when the user click on save button, the data he saved (inserted into a database) to be visible in the previous form (Form1) datagridview, which is not happening. I'm using the code below to insert data->
private void btnAdauga_Click(object sender, EventArgs e)
{
string cs = "Data Source=IS020209;Initial Catalog=TrafficManager;Integrated Security=True";
string insert = "INSERT INTO BILETE (IDPasager, IDCursa, Codbagaj, DataAchizitie, Pret) VALUES (@IDPasager, @IDCursa, @Codbagaj,'"+this.dataBilet.Text+ "', @Pret)";
try
{
if (txtCod.Text.Trim() == "" && grdPasageri.SelectedRows.Count==0 && grdCurse2.SelectedRows.Count==0) { MessageBox.Show("Trebuie sa introduceti codul bagajului si sa selectati un pasager pentru o cursa!"); }
else
{
using (SqlConnection con = new SqlConnection (cs))
{
con.Open();
SqlCommand cmd = new SqlCommand(insert, con);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("IDPasager", int.Parse(grdPasageri.CurrentRow.Cells[0].FormattedValue.ToString().Trim()));
cmd.Parameters.AddWithValue("IDCursa", int.Parse(grdCurse2.CurrentRow.Cells[0].FormattedValue.ToString()));
cmd.Parameters.AddWithValue("Codbagaj", int.Parse(txtCod.Text.Trim()));
cmd.Parameters.AddWithValue("Pret", txtPret.Text);
int val = cmd.ExecuteNonQuery();
MessageBox.Show( val + "Biletul a fost adaugat cu succes pentru cursa selectata!");
con.Close();
this.Dispose();
}
}
}
catch (Exception er) { MessageBox.Show(er.Message); }
}
I've been trying using this.Refresh()
but didn't work.
Please provide some tips. Thanks!