我在Windows窗體中構建應用程序。我的問題是,如果執行下面的代碼中的「else」條件,它不會顯示代碼所暗示的消息框。Windows窗體 - 顯示來自消息框的消息的問題
else
MessageBox.Show(this, "invalid username password")
我在做什麼錯?
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=192.168.6.51,1433;Network Library=DBMSSOCN; Database=INTIME; User Id=********; password=********";
con.Open();
string str = "select * from Login_table where user_name='" + textBox1.Text + "' and password='" + textBox2.Text + "'";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataReader dr = cmd.ExecuteReader();
string login = textBox1.Text;
string pwd = textBox2.Text;
while (dr.Read())
{
if ((dr["user_name"].ToString() == login) && (dr["password"].ToString() == pwd))
{
Form2 objform1 = new Form2();
objform1.Show();
this.Hide();
}
else
MessageBox.Show(this, "invalid username password");
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
您是否嘗試將第一個參數從電話?使它只是'MessageBox.Show(「無效的用戶名密碼」);' –