0

Hie, I'm having a bit of a crisis trying to run an sql query from a C# class. It was working fine in the Login page it was in before and I changed very little of it when I moved it over.

SQL query in class:

    namespace Masca
    {
    public class loginc
        {
        // Telling the class that the page 'Login' exists.
        public Login login;

....

    public void Login ()
    {
        // connection parameters
        string sqlcon = "datasource=localhost;port=3306;username = root; password = root";
        //Command to carry out -This is what the exception highlights when it's thrown.
        string query = "SELECT * FROM logon.login where username = '" + login.username.Text + "' and password = '" + login.password.Password + "';";

        MySqlConnection con = new MySqlConnection(sqlcon);

        MySqlCommand cmd = new MySqlCommand (query,con);

        MySqlDataReader rdr;
        con.Open();
        rdr = cmd.ExecuteReader();
        int count = 0;
        while (rdr.Read())
        {
            count = count + 1;
        }

        if (count == 1)
        {
            //If the username and password match those in the database table, run the method 'login' in the login page cs.
            login.login();
        }

        else
        {
            // if they don't match, run the method 'failLogin' in the login page cs.
            login.failLogin();
        }

    }

In login.cs I have two textboxes and a button. One for the username and one for the password. The button is supposed to trigger the above code:

    public void Logon_Click(object sender, RoutedEventArgs e)
    {
        loginc.Login();
    }

Which it does fine. However I get the classic "Object reference not set to an instance of an object" exception thrown on the query. When I enter the actual credentials instead of '+ login.username.Text +' and '+ login.password.Password +' then the exception gets thrown on 'login.Login()'.

I don't understand why the loginc.cs class refuses to relate to the login.cs but the login.cs clearly has no problem triggering the method in class the same way. Anyone know where I'm going wrong?

1
  • 4
    Storing passwords in plain text AND a SQL Injection vulnerability? Sweet baby jesus educate yourself Commented Apr 30, 2014 at 11:42

1 Answer 1

1

I see no code where you instantiate Login login. You should really do that.

Login login = new Login();

or something alike.

Sign up to request clarification or add additional context in comments.

5 Comments

Okay.. Clearly I missed that. Trying it now.
@ Patrick Hofman - Sort of. I put it right before the connection parameters in the method. I don't get any more exceptions...And it does log in if I put the actual username and password instead of 'login.username.Text' in the code. How ever MySQL always tells me the password is incorrect if I type it in the textboxes.
@Offer: Check where you fill in the variables. Are they data bound? Maybe you should create a new question and show your WPF code if applicable.
Hi again. I I'm not all too familiar with binding but I was under the impression that Passwordboxes couldn't be databound so I didn't really bother trying. Any link you could refer to me? And If I may just inquire, Since the commands have no problem executing between the class and windows but text cant seem to be passed between them, could the issue be that I'm opening new instances where the text does not exist?
@Offer: please create a new question with the code in WPF and the place where you instantiate your variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.