Reputation: 93
This code is to return the value of textbox in the Login form.
public partial class Login : Form
{
public string returnUsername()
{
string username = textBox1.Text;
return username;
}
}
This code is to allow the ChangePass form to show.
public partial class Mainmenu_Employee : Form
{
private void changePasswd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
Login login = new Login();
ChangePass passwd = new ChangePass(login);
passwd.Show();
}
}
This code is to take the username from Login form so that I can change the password of the username.
public partial class ChangePass : Form
{
Login login = null; //parent form
Mainmenu_Employee main = new Mainmenu_Employee();
public ChangePass(Login login1)
{
InitializeComponent();
login = login1;
}
private void buttonChangePass_Click(object sender, EventArgs e)
{
Model_DB_Employee emp = new Model_DB_Employee();
//Login login = new Login();
string username = login.returnUsername();
if (textBoxNewPass.Text == string.Empty || textBoxConfirmPass.Text == string.Empty)
{
MessageBox.Show("Field cannot be empty!");
}
else
{
if (textBoxNewPass.Text == textBoxConfirmPass.Text)
{
try
{
emp.changePasswd(username,textBoxConfirmPass.Text);
MessageBox.Show(username);
MessageBox.Show("Password updated!");
this.Hide();
main.Show();
}
catch(SystemException ex)
{
MessageBox.Show("Password not updated" + ex);
}
}
else
{
MessageBox.Show("Passwords do not match!");
}
}
}
Change password function:
public void changePasswd(string username, string newpass) //change password
{
Model_Employee emp = new Model_Employee();
//Hasher hash = new Hasher(); //call hasher class for hashing
//string hashed;
//string salt = emp.generateSalt(); //generate random salt
//newpass = newpass + salt; //append salt to newpass
//hashed = hash.encryption(newpass); //hash newpass
for (int i = 0; i < rows.Count; ++i)
{
if ((string)empTab.Rows[i]["username"] == username)//check if ID matches
{
empTab.Rows[i]["passwd"] = newpass; //set passwd to hash new password
//check if dataset has changes
if (dataset.HasChanges())
{
//update database
dbAdapter.Update(dataset, "employee");
MessageBox.Show("Employee Updated!");
refreshTable();
}
else
{
refreshTable();
}
}
}
}
I am trying to change a user's password when he is logged in.
When he logs in, I want to capture his username through a textbox.
After he logs in, there will be a main menu displayed.
The user needs to click on the change password link and a change password form will appear.
Therefore, I need to pass the username from the login form to the change password form in order to use a change password function. However, the issue I am facing now is that the username does not get passed from the login form to the change password form.
Upvotes: 1
Views: 1047
Reputation: 216343
You never show the Login form, how do you suppose that someone sets the textBox1 with an actual username?
You need something like this
string username = string.Empty;
Model_DB_Employee emp = new Model_DB_Employee();
using(Login login = new Login())
{
if(DialogResult.OK == login.ShowDialog())
username = login.returnUsername();
}
if(username == string.Empty)
{
MessageBox.Show("Username required");
return;
}
Upvotes: 0
Reputation: 35736
When, between these two lines, does the username
in the new login
get set?
Login login = new Login();
string username = login.returnUsername();
Upvotes: 1
Reputation: 2647
The problem is that with Login login = new Login()
you´re shadowing your class instance variable login
. Try:
private void buttonChangePass_Click(object sender, EventArgs e)
{
Model_DB_Employee emp = new Model_DB_Employee();
string username = login.returnUsername();
if (textBoxNewPass.Text == string.Empty || textBoxConfirmPass.Text == string.Empty)
{
MessageBox.Show("Field cannot be empty!");
}
else
{
if (textBoxNewPass.Text == textBoxConfirmPass.Text)
{
try
{
emp.changePasswd(username,textBoxConfirmPass.Text);
MessageBox.Show(username);
MessageBox.Show("Password updated!");
this.Hide();
main.Show();
}
catch(SystemException ex)
{
MessageBox.Show("Password not updated" + ex);
}
}
else
{
MessageBox.Show("Passwords do not match!");
}
}
}
Upvotes: 1
Reputation: 203825
The problem is the line:
Login login = new Login();
This is shadowing the login
instance field with a new instance of Login
as a local variable, so rather than accessing the Login
created earlier that the user has interacted with, you're accessing a blank one. You can just delete the above line of code.
Upvotes: 3