C#:标签没有显示数据

时间:2015-06-11 10:02:07

标签: c# sql label

{
    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 个答案:

答案 0 :(得分:2)

如果您使用Show显示表单,您的代码就可以使用。但是,ShowDialog是同步的,因此在表单关闭之前不会执行标签分配。只需在调用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();