我有两种形式Form1和Form2。 Form1包含两个用于登录详细信息的文本框,Form2包含一个仅用于显示用户名的标签。我有这个代码,但它不起作用。提前谢谢。
Form1中
private void button1_Click(object sender, EventArgs e)
{
try
{
string constring = @"Data Source=.;Initial Catalog=POS;Integrated Security=True";
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand sqlcmd = new SqlCommand("Select firstname, password from credentials", con);
SqlDataReader dr = sqlcmd.ExecuteReader();
while (dr.Read())
{
string username = usernametxt.Text;
string password = passwordtxt.Text;
if (dr["username"].ToString() == username && dr["password"].ToString() == password)
{
Form2 hmpage = new Form2(username);
hmpage.Show();
this.Hide();
}
else
{
MessageBox.Show("The username or password you entered is incorrect!", "Logon Message!");
}
}
con.Close();
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
窗体2
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text = "Welcome to:(" + strTextBox + ")";
}