-1

I have a single form that is "divided" into two portions. First portion has "User ID" (text box), "Security Question" (combo) and answer to "Security Question" (text box).It has a "Submit Button" which checks database for matching security/Answer. If match is found, second portion below is enabled.

Second portion has "New Password" & "Confirm Password", and "Change" button.

What I want to achieve is verify security question and answer match for the "UserID" above. That I can do. However, I want to pass the "User ID" to the click event of "Change" button, so I do not have to ask for "User ID" again, and do not change password for everyone in the database.

Here is part of my code:

private void btnchange_Click(object sender, EventArgs e)
{
    {
        //How do I pass UserID from "Submit"?
        if (txtnewpassword.Text == "")
        {
            MessageBox.Show("Please enter a new password...");
        }
        if (txtconfirmpassword.Text == "")
        {
            MessageBox.Show("Please confirm your new password...");
        }
        else
        {
            try
            {
                SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLlocaldb; Initial Catalog = AdminAuthentication; Integrated Security = True");
                con.Open();

                if (txtnewpassword.Text == txtconfirmpassword.Text)
                {
                    string query = " update UserRegistration set [Password]= '" + txtnewpassword.Text + "' where UserID = '" /*+pass USERID here?*/ + "'";
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Password Succesfully Updated!");
                    txtconfirmpassword.Clear();
                    txtnewpassword.Clear();
                    txtuserid.Clear();
                    this.Hide();
                    Loginsystem ls = new Loginsystem();
                    ls.Show();
                }
                else
                {
                    MessageBox.Show("The passwords do not match.Try again");
                    txtconfirmpassword.Clear();
                    txtnewpassword.Clear();
                }

                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Occured" + ex);
            }
        }
    }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Joel Bwana
  • 43
  • 6

1 Answers1

0

Ciao, I would raise an event in the class that holds UserID. Then the depending class can subscribe to that event and store data in a property that will be read in btnchange_Click function.

public delegate void UserIDHandler(int UserID);
public class UserIDClass
{
    public event UserIDHandler UserIDEvent;
    ...
    public void Submit(object sender, EventArgs)
    {
       // retrieve your UserID then 
       if(UserIDEvent!= null)
          UserIDEvent(UserID);
    }
 }

 public class BtnChangeClass
 {
     public int UserID_imported = -1;
     UserIDClass yourObject = new UserIDClass();
     

     public BtnChangeClass()
     {
          yourObject.UserIDEvent += new UserIDHandler(hadleUserIDEvent);
     }

     protected void hadleUserIDEvent(int UserID)
     {
         UserID_imported = UserID;
     }
     
     private void btnchange_Click(object sender, EventArgs e) {
         //read UserID_imported
     }
}
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
  • It's confusing me that you didn't use the class name `Loginsystem` in your answer. Can you please make it clear which of your classes in the `Loginsystem` form that the OP is using? – Enigmativity Jul 20 '20 at 23:59