C# : Label is not showing data

77 Views Asked by At
{
    SqlConnection con2 = new SqlConnection("Data Source=jayi-pc\\sqlexpress;Initial Catalog=DB_test1;Integrated Security=True");
        con2.Open();
        SqlCommand sc3 = new SqlCommand(@"Select * from Visitor_1");
        sc3.Connection = con2;
        SqlDataReader dr3 = sc3.ExecuteReader();

        dr3.Read();
        Form2 frm2 = new Form2();
        frm2.ShowDialog();
        this.Hide();
        frm2.label1.Text = dr3["Id"].ToString();
        frm2.label2.Text = dr3["Name"].ToString();
        frm2.label3.Text = dr3["Address"].ToString();
        frm2.label4.Text = dr3["Purpose"].ToString();
        frm2.label5.Text = dr3["Phone No"].ToString();
        dr3.Close();
        con2.Close();
    }
1

There are 1 best solutions below

0
On

Your code would work if you were displaying the form with Show. However, ShowDialog is synchronous, therefore your label assignment isn't executed until the form is closed. Just move that bit of code before the call to ShowDialog.

this.Hide();
frm2.label1.Text = dr3["Id"].ToString();
frm2.label2.Text = dr3["Name"].ToString();
frm2.label3.Text = dr3["Address"].ToString();
frm2.label4.Text = dr3["Purpose"].ToString();
frm2.label5.Text = dr3["Phone No"].ToString();
frm2.ShowDialog();