-2

I'm trying to run a query in MySQL database with C#. I want the column prioritySettings returned when the 3 previous columns are within certain values. This query works, but only with one variable at a time, so at the moment I can only specify the value of one of the columns. Are my AND statements correct?

string query = "SELECT prioritySetting FROM {DATABASE} WHERE handling ='" + handling + "'" + "AND corner ='" + corner + "'" + "AND power  ='" + power + "'";

some more code;

                 MySqlDataReader sqlReader;                                       
                string handling = overOrUnderInput;
                string corner = cornerPartInput;
                string power = onOrOffPowerInput;
                string query = "SELECT prioritySetting FROM {DATABASE} WHERE handling ='" +             handling + "'" + " AND corner ='" + corner + "'" + " AND power  ='" + power + "'"; 
                MySqlCommand getRecords = new MySqlCommand(query, connection);
                connection.Open();
                sqlReader = getRecords.ExecuteReader();

                    while (sqlReader.Read())
                    {

                        try
                        {

                            try
                            {

                                suggestions[i] = (sqlReader.GetString(0));
                            }
                            catch
                            {
                            }
                            i++;

                        }
                        catch (MySqlException ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

and the error;

System.NullReferenceException: Object reference was not set to an instance of an object.

EPicLURcher
  • 21
  • 1
  • 7

4 Answers4

4

Seem like a space is required before each AND.

İsmet Alkan
  • 5,141
  • 3
  • 38
  • 64
0

Add a space before each AND

string query = "SELECT prioritySetting FROM {DATABASE} WHERE handling ='" + handling + "'" + " AND corner ='" + corner + "'" + " AND power  ='" + power + "'";
Tushar
  • 11,306
  • 1
  • 21
  • 41
0

Let's see how your query looks if the parameters are inserted. I assume parameters have values equal to their names.

SELECT prioritySetting 
FROM {DATABASE} 
WHERE handling ='handling'AND corner ='corner'AND power  ='power'

If it's really DATABASE after the FROM you should change it to TABLE. I'm not very sure if it causes any problems, but I strongly recommend putting spaces before ANDs. So your code line will be like:

string query = "SELECT prioritySetting FROM {DATABASE.TABLE} WHERE handling ='" + handling + "'" + " AND corner ='" + corner + "'" + " AND power  ='" + power + "'";
İsmet Alkan
  • 5,141
  • 3
  • 38
  • 64
  • tried inserting them "manually" and i got the same error (null exception when tried to save into array) as when they were being inserted by a variable – EPicLURcher Oct 13 '14 at 08:30
  • can you please provide your table schema and some more code. (especially where it throws the error) – İsmet Alkan Oct 13 '14 at 08:32
0
string query = "SELECT prioritySetting FROM {TABLE}
                    WHERE handling ='" + handling + "'" + " 
                    AND corner ='" + corner + "'" + "
                    AND power  ='" + power + "'";
sqluser
  • 5,145
  • 7
  • 32
  • 48