我是一名C#学习者,需要您的帮助。提前致谢。我正在处理此多用户单次登录表单。我已连接到SQL本地数据库,其中包含以下内容: 用户名,密码,用户类型
我的问题是,只要这三个是正确的,下面的代码就可以正常工作。例如,如果我输入了错误的密码,则不会发生任何事情。
我的代码的“ Else”部分一定存在问题,但是我尝试了很多更改,甚至在我接近使之工作时也失去了跟踪。请帮忙。 重申我对项目的期望: 1.从组合框(cmbusertype)中选择“管理员”或“学生”并提供不正确的用户名和密码时,我会收到不正确的凭据错误消息和尝试次数。3次尝试后,登录按钮应显示为灰色,并提示我重置我的密码。
public partial class Loginsystem : Form
{
public Loginsystem()
{
InitializeComponent();
}
int attempts =1;
private void btnlogin_Click(object sender, EventArgs e)
{
if (cmbusertype.SelectedItem == null)
{
MessageBox.Show("Please select User Type to continue...");
cmbusertype.Focus();
return;
}
if (txtuserid.Text == "")
{
MessageBox.Show("Please enter your UserID...");
txtuserid.Focus();
return;
}
if (txtpassword.Text == "")
{
MessageBox.Show("Please enter your password...");
txtpassword.Focus();
return;
}
try
{
SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLlocaldb; Initial Catalog = AdminAuthentication; Integrated Security = True"); ;
SqlCommand cmd = new SqlCommand("select * from SimplifyLogin where userID='" + txtuserid.Text + "' and password='" + txtpassword.Text + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
string cmbItemValue = cmbusertype.SelectedItem.ToString();
if (dt.Rows.Count >0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["UserType"].ToString() == cmbItemValue) //you can use 2 instead of usertype in that index because usertype column is in 2 index
{
if (cmbusertype.SelectedIndex == 0)
{
MessageBox.Show("You are logged in as " + dt.Rows[i][2]);
MessageBox.Show("Displaying Admin Dashboard");
this.Hide();
}
else
{
MessageBox.Show("Welcome Student! Displaying Exam Options");
this.Hide();
}
}
else
{
MessageBox.Show("Invalid username and/or Password.Please try again. \nAttempts: " + attempts + "out of of 3");
txtpassword.Clear();
attempts++;
}
if (attempts == 4)
{
MessageBox.Show("You have reached maximum login attempts. Click 'Forgot Password' below to reset it.");
btnlogin.Enabled = false;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
答案 0 :(得分:0)
我可以自定义此代码,因为您不必使用for循环,因为从数据库返回的行数仅是一个。.
public partial class Loginsystem : Form {
public Loginsystem() {
InitializeComponent();
}
private int attempts = 1;
private void btnlogin_Click(object sender, EventArgs e) {
if (cmbusertype.SelectedItem == null) {
MessageBox.Show("Please select User Type to continue...");
cmbusertype.Focus();
return;
}
if (txtuserid.Text == "") {
MessageBox.Show("Please enter your UserID...");
txtuserid.Focus();
return;
}
if (txtpassword.Text == "") {
MessageBox.Show("Please enter your password...");
txtpassword.Focus();
return;
}
try {
SqlConnection con = new
SqlConnection(@"Data Source = (LocalDB)\MSSQLlocaldb; Initial Catalog = AdminAuthentication; Integrated Security = True"); ;
SqlCommand cmd = new SqlCommand("select * from SimplifyLogin where userID='" + txtuserid.Text + "' and password='" + txtpassword.Text + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
string cmbItemValue = cmbusertype.SelectedItem.ToString();
if (dt.Rows.Count > 0) {
if(dt.Rows[0]["UserType"].ToString() == cmbItemValue) {
if (cmbusertype.SelectedIndex == 0 ) {
MessageBox.Show("You are logged in as " + dt.Rows[0][2] + Environment.NewLine + "Displaying Admin Dashboard");
this.Hide();
} else {
MessageBox.Show("Welcome Student! Displaying Exam Options");
this.Hide();
}
} else {
MessageBox.Show("Invalid username and/or Password.Please try again. \nAttempts: " + attempts + "out of of 3");
txtpassword.Clear();
attempts++;
if (attempts == 4) {
MessageBox.Show("You have reached maximum login attempts. Click 'Forgot Password' below to reset it.");
btnlogin.Enabled = false;
}
}
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
答案 1 :(得分:0)
我今天早上想通了:
这是我所做的更改:
摆脱了一个嵌套的ifs
if(((dt.Rows [i] [“ UserType”]。ToString()== cmbItemValue)&&(cmbusertype.SelectedIndex == 0))
将}调整为必要。