1

I need help to call a function while passing strings from one form to another when clicking a button.

Form1 (Form1) has the MySQL connection and "Select" statements needed to verify the login.

Form2 (LoginWindow) has the Login user and password TextBoxes and Login button.

The error I get is: "Cannot implicitly convert type 'void' to 'Application.Main.App.Form1.MyFunctionExposed'"

Here is what I have:

Form1.cs:

namespace Application.Main
{


  public partial class Form1 : Form 
  {
    public string strUser;
    public string strPass;

    public delegate void MyFunctionExposed();
    public static MyFunctionExposed functionMySQLCheckLogin;


    public Form1()
    {
        InitializeComponent();


        functionMySQLCheckLogin = CheckLogin(strUser, strPass);

    }




   //Check Login
   public void CheckLogin(string strUser, string strPass)
   {
       try
       {
           //Open connection
           string query = "SELECT * FROM users WHERE user_name = '" + strUser + "' AND user_password = '" + strPass + "' ";
           //Create Command
           MySqlCommand cmd = new MySqlCommand(query, connection);
           //Create a data reader and Execute the command and the rest of the MySQL query code


       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
   }
  }
}

Form2.cs

namespace Application
{
public partial class LoginWindow : Form
    {

         public LoginWindow()
        {
           InitializeComponent();
        }

        private void LoginWindow_Load(object sender, EventArgs e)
        {

        }


        private void btnLoginGo_Click(object sender, EventArgs e)
        {
            Application.Main.Form1.functionMySQLCheckLogin(inputBoxLoginUser.Text, inputBoxLoginPassword.Text);
        }
 }

Any ideas how to fix this or do it differently using these two Forms? Thanks.

AJ152
  • 671
  • 2
  • 12
  • 28

1 Answers1

1

To get this to compile, you just need to get the parameters correct - add the (string, string) signature to the delegate, and the assignment of the static is to the method group (i.e. not invoking the function):

    public delegate void MyFunctionExposed(string user, string pass);
    public static MyFunctionExposed functionMySQLCheckLogin;

    public Form1()
    {
        InitializeComponent();

        functionMySQLCheckLogin = CheckLogin;
    }

However, presumably the reason this came up is because you want to reuse the code. If so, I would consider refactoring and moving the CheckLogin code to a more suitable class, e.g. a common helper, data access, or even a base form class (if it is to be used by multiple forms).

Another alternative would be to make the CheckLogin method static (since it has no dependencies on the state of Form1 fields) - this way you can just call it from anywhere as Form1.CheckLogin(.., ..)

Last thing - please look at parameterizing the sql query as it stands, it is susceptible to sql injection attacks, and login screens are a favourite target.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285