-1

I'm pretty sure that the Sql Syntax is right since it's a legit query. However i've never stumbled on this issue before.

private void button1_Click(object sender, EventArgs e)
{
    string ett = textBox1.Text;
    if (ett == "")
    {
        MessageBox.Show("Du måste fylla i UID, vilket du finner i användarlistan.");
        return;
    }
    try
    {
        if (connect.State == ConnectionState.Open)
        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = connect;
            cmd.CommandText = "DELETE FROM Users WHERE uid = @uid";
            cmd.Parameters.AddWithValue("@uid", textBox1.Text);
            MySqlDataReader accessed = cmd.ExecuteReader();
            MessageBox.Show("Användaren borttagen.");
        }
        else
        {
            MessageBox.Show("Något gick tyvärr fel, kontakta systemadministratören.");
        }
    }
    catch (Exception ex)
    {
        { MessageBox.Show(ex.Message); }
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Hajpz
  • 41
  • 8
  • 3
    Where do you get the exception? Have you read this: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it ? Also, use `cmd.ExecuteNonQuery();` instead of `ExecuteReader`. – Tim Schmelter Oct 26 '15 at 15:07
  • Just run it in debug and find which variable is null. My bets are on `connect` - where is it declared/initialised? – Bridge Oct 26 '15 at 15:08
  • debug it first, then tell us where do you get the exception. – jLaw Oct 26 '15 at 15:25
  • 1
    Also, try not to check ett = "" ... better use string.IsNullOrEmpty(ett), and put try and catch block inside of IF ... that way your program will never try to access your base without ett value – Veljko89 Oct 26 '15 at 15:29
  • 1
    consider wrapping your sql command, connection..etc around a `using(){}` statement also read up on how to execute `INSERTS, UPDATES, & DELETES` in regards to knowing the difference between `ExecuteNonQuery method and ExecuteReader method` one updates while the other returns data.. – MethodMan Oct 26 '15 at 15:37
  • pastebin.com/DJ8pGJ67 here you have the whole cs file. – Hajpz Oct 27 '15 at 07:30
  • Possible duplicate of [Object reference, c#](http://stackoverflow.com/questions/33362696/object-reference-c-sharp) – Bridge Jan 08 '16 at 15:01

3 Answers3

0

The problem may be related to this:

        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = connect;
            cmd.CommandText = "DELETE FROM Users WHERE uid = @uid";
            cmd.Parameters.AddWithValue("@uid", textBox1.Text);
            MySqlDataReader accessed = cmd.ExecuteReader();
            MessageBox.Show("Användaren borttagen.");
        }

try

        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = connect;
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "DELETE FROM Users WHERE uid = @uid";
            cmd.Parameters.AddWithValue("@uid", textBox1.Text);
            cmd.ExecuteNonQuery
            MessageBox.Show("Användaren borttagen.");
        }
David BS
  • 1,822
  • 1
  • 19
  • 35
0

Now you've shown us your whole code in the comments, the problem is obvious.

You have written a method to initialise, set up and open your database connection; and this other method which runs on a button click, which uses it.

However, nowhere in your code do you call the method which initialises your database connection, therefore it is not set up when you try to use it - obvious really.

I can see you think you are checking to see if the connection is working by checking its State property, but calling any sort of method or property accessor on an uninitialised reference type won't work, you'll get the NullReferenceException you've been getting.

To fix, call the connection set up method from your button press, before trying to use the connection:

private void button1_Click(object sender, EventArgs e)
{
    string ett = textBox1.Text;
    if (ett == "")
    {
        MessageBox.Show("Du måste fylla i UID, vilket du finner i användarlistan.");
        return;
    }
    try
    {
        db_connection(); //added this line
        if (connect.State == ConnectionState.Open)
        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = connect;
            cmd.CommandText = "DELETE FROM Users WHERE uid = @uid";
            cmd.Parameters.AddWithValue("@uid", textBox1.Text);
            MySqlDataReader accessed = cmd.ExecuteReader();
            MessageBox.Show("Användaren borttagen.");
        }
        else
        {
            MessageBox.Show("Något gick tyvärr fel, kontakta systemadministratören.");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }   
}
Bridge
  • 29,818
  • 9
  • 60
  • 82
-1

You have not defined the variable, "connect".

Tyler Pantuso
  • 835
  • 8
  • 16
  • Can you see the full class somehow? I can't. Of course it's possible that `connect` isn't initialized, but that's just guessing. Even the first variable `textBox1` could be `null`. – Tim Schmelter Oct 26 '15 at 15:14
  • What else would it be? To find the problem, you would click on "connect", and hit F12. That's where the problem will be. – Tyler Pantuso Oct 26 '15 at 15:23
  • If `textBox1` were null, there would be a big red line underneath it. I'm sure Hajpz would have seen that. – Tyler Pantuso Oct 26 '15 at 15:30