'Password reset with button click to save onto database
I'm quite amateurish when it comes to coding, I'm trying code into a button click where it resets the users password with an computer generated 15 digit code, and then stores it in the database. currently its not doing what's its suppose to be doing, but I'm not getting any error messages to say where I'm going wrong.
public Dashboard()
{
InitializeComponent();
}
private void btnLogOut_Click(object sender, EventArgs e)
{
string text = ControlID.txtBoxUserName;
string text1 = ControlID.txtPassword;
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DAVER\OneDrive\Desktop\Project\LoginDatabase.mdf;Integrated Security=True");
conn.Open();
string pwd = GetRandomAlphanumericString(15);
sqlData.Parameters.AddWithValue("@password", pwd);
SqlCommand sqlData = new SqlCommand("UPDATE Login Password ='" + text1 + "' WHERE VALUES @password'", conn);
conn.Close();
}
public static string GetRandomAlphanumericString(int length) {
const string alphanumericCharacters =
"ABCDEFGHJKLMNPQRSTUVXYZ" +
"abcdefghjklmnpqrstuvwxyz" +
"23456789" +
"!£$%^&*";
return GetRandomString(length, alphanumericCharacters);
}
public static string GetRandomString(int length, IEnumerable<char> characterSet) {
var characterArray = characterSet.Distinct().ToArray();
var bytes = new byte[length * 15];
new RNGCryptoServiceProvider().GetBytes(bytes);
var result = new char[length];
for (int i = 0; i < length; i++) {
ulong value = BitConverter.ToUInt64(bytes, i * 15);
result[i] = characterArray[value % (uint)characterArray.Length];
}
return new string(result);
}
private void Dashboard_Load(object sender, EventArgs e)
{
label1.Text = ControlID.txtBoxUserName;
this.Text = "Welcome " + ControlID.txtBoxUserName;
GetRandomAlphanumericString(15);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
