2014-06-05 80 views
2

我想知道如果有人能指出我在正确的方向。我的程序有1个下拉列表,2个文本框和2个按钮。下拉列表属性

namespace passwordReset 
{ 
    public partial class Form1 : Form 
    { 
     //variables to mess with the password 
     public string password1; 
     public string password2; 
     public string username; 

     public Form1() 

     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      SqlConnection connection = new SqlConnection(xxxxxxx); 
      connection.Open(); 
      string query = "select Login, Password from Employees order by Login desc"; 
      SqlDataAdapter da = new SqlDataAdapter(query, connection); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "Credentials"); 
      ddlLogin.DisplayMember = "Login"; 
      ddlLogin.ValueMember = "Password"; 
      ddlLogin.DataSource = ds.Tables["Credentials"]; 
      connection.Close(); 

     } 

     private void ddlLogin_SelectedIndexChanged(object sender, EventArgs e) 
     { 


      if (ddlLogin.SelectedItem != null) 
      { 
       DataRowView drv = ddlLogin.SelectedItem as DataRowView; 
       //MessageBox.Show("The username you selected is: " + drv.Row["Login"].ToString()); 
       //MessageBox.Show("The password you selected is: " + drv.Row["Password"].ToString()); 
       //MessageBox.Show("username selected is: " + ddlLogin.Text.ToString()); 
       //MessageBox.Show("password is: " + ddlLogin.SelectedValue.ToString()); 
      } 

     } 

     private void txtPassword1_TextChanged(object sender, EventArgs e) 
     { 
      password1 = txtPassword1.Text; 

     } 

     private void txtPassword2_TextChanged(object sender, EventArgs e) 
     { 
      password2 = txtPassword2.Text; 
     } 

     private void btnReset_Click(object sender, EventArgs e) 
     { 
      if (ddlLogin.Text == "rruales" || ddlLogin.Text == "xxxxx" || ddlLogin.Text == "xxxxxx") 
      { 
       MessageBox.Show("Cannot change this user's password"); 

      } 
      if (password1 == password2 && ddlLogin.Text != "rruales" && ddlLogin.Text != "xxxxx" && ddlLogin.Text != "xxxxx") 
      { 
       string newPassword = txtPassword2.Text; 
       username = ddlLogin.Text.ToString(); 
       string currentPassword = ddlLogin.SelectedValue.ToString(); 
       currentPassword = newPassword; 
       SqlConnection connection = new SqlConnection(xxxxxxxx); 
       SqlCommand cmd = new SqlCommand(); 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = "UPDATE Employees SET [Password] = @password WHERE [Login] = @login"; 
       cmd.Parameters.AddWithValue("@password", currentPassword); 
       cmd.Parameters.AddWithValue("@login", username); 
       cmd.Connection = connection; 

       connection.Open(); 
       cmd.ExecuteNonQuery(); 
       MessageBox.Show("Password successfully updated"); 
       connection.Close(); 


      } 

      else 
      { 
       MessageBox.Show("You either choose usernames rruales or xxxxx or xxxx, or the passwords don't match, try again"); 
      } 
     } 

     private void btnClose_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    } 
} 

的代码做的事情需要做,当用户选择从下拉菜单中用户名,就可以重新设置用户的password.But如果用户键入他们要重置的用户名,我得到一个错误的位置:

string currentPassword = ddlLogin.SelectedValue.ToString(); 

错误说对象引用未设置为一个object.use“新”关键字实例创建对象instance.I理解错误是一个事实,即用户输入来用户名而不是选择它。我的问题是,我不需要代码,我想了解如何继续并处理,用户只需键入用户名或从下拉列表中选择它?任何建议重写代码是值得欢迎的,我我是一名入门级开发人员。

更新,我不能回答我的问题,但现在它工作的感谢所有

所有, 感谢你的帮助。 大家所说的什么工作,我也不得不做1只改变我的代码,我意识到我是在做一件很愚蠢的:

private void txtPassword1_TextChanged(object sender, EventArgs e) 
    { 
     password1 = txtPassword1.Text; 

    } 

    private void txtPassword2_TextChanged(object sender, EventArgs e) 
    { 
     password2 = txtPassword2.Text; 
    } 

    private void btnReset_Click(object sender, EventArgs e) 
    { 
     if (ddlLogin.SelectedValue == null) 
     { 
      username = ddlLogin.Text.ToString(); 
     } 
     else 
     { 
      username = ddlLogin.Text.ToString(); 
     } 

     if (ddlLogin.Text == "rruales" || ddlLogin.Text == "xxxxx" || ddlLogin.Text == "xxxxxx") 
     { 
      MessageBox.Show("Cannot change this user's password"); 

     } 
     if (password1 == password2 && ddlLogin.Text != "rruales" && ddlLogin.Text != "xxxxxx" && ddlLogin.Text != "xxxxxx") 
     { 
      string newPassword = txtPassword2.Text; 



      //username = ddlLogin.Text.ToString(); 
      // string currentPassword = ddlLogin.SelectedValue.ToString(); 


      currentPassword = newPassword; 
      SqlConnection connection = new SqlConnection(xxxxxx); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "UPDATE Employees SET [Password] = @password WHERE [Login] = @login"; 
      cmd.Parameters.AddWithValue("@password", currentPassword); 
      cmd.Parameters.AddWithValue("@login", username); 
      cmd.Connection = connection; 

      connection.Open(); 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Password successfully updated"); 
      connection.Close(); 


     } 

     else 
     { 
      MessageBox.Show("You either choose usernames rruales or xxxxx or xxxx, or the passwords don't match, try again"); 
     } 
    } 

    private void btnClose_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
} 

}

我不知道为什么我这样做:

string currentPassword = ddlLogin.SelectedValue.ToString(); 
+1

那么如果用户输入没有选定的值。所以你应该检查它为空,如果是这样的话使用ddl的文本属性。 – TaW

+0

如果用户输入名称而不是从列表中选择,那么selecteditem属性将不会被设置,它将为空。如果是这种情况,那么尝试根据ddlLogin.text属性在列表中找到该项目(它看起来像我在解释@ TaW) –

回答

0

如果您没有从DropDown中选择一个项目,它的SelectedValue将为null。你应该检查它是否为空。如果它为空,则从文本框中获取值。

string userName; 
if (ddlLogin.SelectedValue == null) { 
    userName = theTextBox.Text; 
} else { 
    username = theDropDownList.SelectedValue.Text; 
} 

我不确定它是否是您试图获取的用户名。您提到输入用户名时会抛出异常,但是您从ddlLogin获取密码?无论你想分配什么,只要检查下拉列表是否像上面那样是空的并分配给正确的变量。

+0

很酷,我会试试看,谢谢你尼尔 –