[英]How to show username on the welcome page
I have two forms Form1 and Form2.我有两个表格 Form1 和 Form2。 Form1 contains two textBoxs for login details and Form2 contain a label just to show the username.
Form1 包含两个用于登录详细信息的文本框,Form2 包含一个标签仅用于显示用户名。 I have this code but it doesn't work.
我有这个代码,但它不起作用。 Thanks in advance.
提前致谢。
Form1表格1
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);
}
}
Form2表格2
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text = "Welcome to:(" + strTextBox + ")";
}
It would be better if you add breakpoints to some statements of your code for you to trace why it doesn't work.如果您在代码的某些语句中添加断点,以便跟踪为什么它不起作用,那就更好了。 (press F5.. then F11 after adding breakpoints).
(添加断点后按 F5.. 然后按 F11)。
By then, you can add global variable, which can reside at Program.cs and assign the retrieved username.届时,您可以添加全局变量,该变量可以驻留在 Program.cs 并分配检索到的用户名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.