0

I have some issue's, ive seen a peace of code on the internet only this whas done with Oledb.

Now i have rewrited the code for MySQL.data.mysqlclient as followed:

MySqlDataReader dr = null;
        MySqlCommand cmd = null;
        string cmdstr = "SELECT * FROM users WHERE email='"+UsrName.Text+"' and password='"+PassWrd.Text+"' LIMIT 1";
        dr = cmd.ExecuteNonReader();
        cmd = new MySqlCommand(cmdstr, connection);
        cmd.Dispose();

        if (dr.Read() == true)
        {
            MessageBox.Show("Succesvol ingelogd");
        }
        else
        {
            MessageBox.Show("Geen juiste gegevens");
        }
        connection.Close();

    }

Now the problem is another method for dr = cmd.ExecuteNonReader().

UPDATE----------------

string server;
        string database;
        string uid;
        string password;
        server = "localhost";
        database = "cmstt";
        uid = "root";
        password = "";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        MySqlConnection connection = new MySqlConnection(connectionString);

        connection.Open();

        MySqlDataReader dr = null;
        MySqlCommand cmd = null;
        string cmdstr = "SELECT * FROM users WHERE email='"+UsrName.Text+"' and pass='"+PassWrd.Text+"' LIMIT 1";
        dr = cmd.ExecuteReader();
        cmd = new MySqlCommand(cmdstr, connection);


        if (dr.Read() == true)
        {
            MessageBox.Show("Succesvol ingelogd");
        }
        else
        {
            MessageBox.Show("Geen juiste gegevens");
        }
        cmd.Dispose();
        connection.Close();
2
  • 1
    Your code clearly open to SQL Injection. Please use parameterized SQL like cmd.Parameters.Add("@email", email); Commented Dec 9, 2012 at 17:30
  • Oh yeah, Little Bobby tables. imgs.xkcd.com/comics/exploits_of_a_mom.png Commented Dec 9, 2012 at 17:35

2 Answers 2

2

It will be ExecuteReader() not ExecuteNonReader()

// mention your connection and connection string here.

        connection.open()

        SqlDataReader dr = null;
        MySqlCommand cmd = null;
        string cmdstr = "SELECT * FROM users WHERE email='"+UsrName.Text+"' and password='"+PassWrd.Text+"' LIMIT 1";

        cmd = new MySqlCommand(cmdstr, connection);
        dr = cmd.ExecuteReader();

        if (dr.Read() == true)
        {
            MessageBox.Show("Succesvol ingelogd");
        }
        else
        {
            MessageBox.Show("Geen juiste gegevens");
        }
        connection.Close();

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

2 Comments

dr = cmd.ExecuteReader(); nullReferenceException was unhandled : Object reference not set to an instance of an object.
make sure you have open connection of sql server.
0

You are disposing cmd prematurely. Dispose it just before you close the connection.

1 Comment

connection is open and cmd is desposed before connection close. Still have the error

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.