1

I am currently getting an error when trying to get data from mySQL:

Additional information: Could not find specified column in results: admin

My code is:

public int getLevel()
    {
        string sqlCommand = "Select level from users where username = 'admin'";
        int value = 0;

        MySqlConnection con = new MySqlConnection("host=111.222.111.222;user=MYUSERNAME;password=MYPASSWORD;database=tcg;");
        MySqlCommand cmd = new MySqlCommand(sqlCommand, con);

        con.Open();

        MySqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            value += int.Parse(reader.GetString("admin"));
        }
        return value;
    }
8
  • you are only bringing back column level Commented Sep 21, 2016 at 21:01
  • 1
    Before you start coding too much, utilize using blocks for safe resource use. Otherwise all your code will look like the above :p Commented Sep 21, 2016 at 21:03
  • Yeah, I'm trying to get the data of level Commented Sep 21, 2016 at 21:04
  • 1
    Then why do you have GetString("admin")); Commented Sep 21, 2016 at 21:04
  • If you have never worked with resultsets before then just say so and someone can maybe type up an answer. But it is like Hour 1 of a tutorial and stackoverflow is not that kind of a site Commented Sep 21, 2016 at 21:11

1 Answer 1

1

Try:

 public int getLevel()
 {
     int value = 0;
     using(MySqlConnection con = new MySqlConnection("host=45.37.80.181;user=MYUSERNAME;password=MYPASSWORD;database=tcg;"))
     {
         con.Open();
         using(MySqlCommand cmd = con.CreateCommand())
         {
             cmd.CommandText = "Select level from users where username = @ad";
             cmd.Parameters.AddWithValue("@ad","admin");
             MySqlDataReader reader = cmd.ExecuteReader();

             while (reader.Read())
             {
                 value += int.Parse(reader[0]);
             }
        }
        con.Close();
     }
     return value;
}

If you only have one admin, use:

public int getLevel()
 {
     int value = 0;
     using(MySqlConnection con = new MySqlConnection("host=45.37.80.181;user=MYUSERNAME;password=MYPASSWORD;database=tcg;"))
     {
         con.Open();
         using(MySqlCommand cmd = con.CreateCommand())
         {
             cmd.CommandText = "Select level from users where username = @ad";//add Order by if you need to
             cmd.Parameters.AddWithValue("@ad","admin");
             value += Convert.ToInt32(com.ExecuteScalar());//this assumes you will get an integer value

        }
        con.Close();
     }
     return value;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure why you got a DV. Here is one up. Thx for helping the op
'cause I put TOP in the SELECT statement which is SQL not MySQL. Someone got their panties in a bunch before I could edit it. No worries - glad it helped. Cheers!

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.