5

I'm not familiar with using Data Reader, i need help with the following code, i want to retrieve a single data from the database.

MySqlDataAdapter data = new MySqlDataAdapter(cmd);
                    conn.Open();
                    DataTable dt = new DataTable();
                    data.Fill(dt);
                    gridView1.DataSource = dt;

                    int retrievedValue = 0;
                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if ((int)reader["order_status"] == 0)
                            {
                                retrievedValue = (int)reader.GetValue(0);

                                    GridView View2 = sender as GridView;
                                    e.Appearance.BackColor = Color.Green;
                                    e.Appearance.BackColor2 = Color.ForestGreen;
                            }
                        }
                    }
2
  • What is the type of order_status column? Also you need to use == for equality checking, = is assignment operator. Commented Apr 16, 2015 at 13:11
  • It contains a numeric values 1, 2, 3, 4, 5 its an INT Commented Apr 16, 2015 at 13:14

2 Answers 2

5

reader["order_status"] returns object, since you told it is an already integer, you need to cast it to int first.

You need to use == operator as well since it is a equality operator. = operator is an assignment operator.

if ((int)reader["order_status"] == 0)

Or you can use GetInt32 method with it's zero-based column number. Let's say it's the first column that your query returns, you can use it like;

if(reader.GetInt32(0) == 0)

By the way, if you wanna get only single value, I strongly suspect you may wanna use ExecuteScalar method since it get's the first column of the first row. Then you can structure your query as SELECT order_status FROM ... etc..

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

3 Comments

@KevinRodriguez If value that you want to get is the first column of the first row, you can use ExecuteScalar method like, int value = (int)cmd.ExecuteScalar();. Remember, your query can get multiple rows with multiple columns. This does not break this methods. ExecuteScalar method gets first column of the first row and other values in your command are ignored.
im not getting this right, the code wont let me retrieve rows that has a value of 0?
What is your cmd query exactly? You will not need ExecuteReader when you use ExecuteScalar by the way.
1

Be sure to assign a variable before while (reader.Read()) otherwise it will and error. Then close the data reader once you are finished using it. Like so:

 using (MySqlDataReader reader = cmd.ExecuteReader())    
    {

    int retrievedValue = 0;

          while (reader.Read())
          {
                retrievedValue = (int)reader.GetValue(0);
                if (retrievedValue == 0)
                {

                    GridView View2 = sender as GridView;
                                    e.Appearance.BackColor = Color.Green;
                                    e.Appearance.BackColor2 = Color.ForestGreen;
                }
                else if (retrievedValue == 1)
                {

                    GridView View2 = sender as GridView;
                                    e.Appearance.BackColor = Color.Red;
                                    e.Appearance.BackColor2 = Color.ForestGreen;
                }
          }//and so on...
          reader.Close();
    }

I hope this is was you're looking for.

6 Comments

thank you, will this code retrieve rows that has a value of 0?
Which column number do you want to retrieve data from? If you want to retrieve from the first column then use retrievedValue = dr.GetValue(0) or from second column, retrievedValue = dr.GetValue(1), etc. Note that it will only use 1 row but you must specify which column you want in dr.GetValue.
You may also need to convert data to a data type such as: Convert.ToInt32(dr.GetValue(0)); but for strings, you do not: dr.GetString(0)
order_status column, it has a value of ranging from 1 - 6, and if the value of a row is 1 i need to paint the gridview row to red and if it is 2 need to paint it with green and so on. im getting an error Could not find specified column in results: order_status
The error is likely caused by misspellings in your query. Make sure that your column names, tables, etc are correctly spelled.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.