0

I'am having a hard time with C# ATM...

Its about this nasty piece of code here:

foreach (DataGridViewRow r in dgv_selectedOrders.Rows)
            {
                MessageBox.Show(r.Cells[0].Value.ToString());
                order_id = r.Cells[0].Value.ToString();
                query = "SELECT count(orders.id) AS total_orders FROM orders WHERE customer_id = (SELECT customer_id FROM orders WHERE id = " + order_id + ");";
                command = new MySqlCommand(query, conn);
                MySqlDataReader sda = command.ExecuteReader();
                while (sda.Read())
                    dgv_selectedOrders[11,r.Index].Value = sda.GetInt32(0).ToString();*/

            }

This loop is going through only twice and then it stops...

Interesting is, that when I uncomment everything execpt the "MessageBox" it nicely loops through each row and it displays every "order_id" of my DataGridView.

What it should do is:

Loop through existing DataGridView and get the "Order_id" from each row. Then I want to count how many orders are already made by the customer and append the result in coulmn 11 of the DataGridView...

Any ideas?

As I said, it stops after the second row ...

  • What happens if you generate your string first, and then show it in the message box? Have you debugged line-by-line? – Scott Solmer Nov 06 '14 at 20:01
  • Hey man. Yea I did. The problem seems to be the MySqlDataReader line. It stops there. The Query is also good to go. hmmm... – Michael Nov 07 '14 at 07:14
  • But to answer your question fully its: SELECT count(orders.id) AS total_orders FROM orders WHERE customer_id = (SELECT customer_id FROM orders WHERE id = 111) and the second is: SELECT count(orders.id) AS total_orders FROM orders WHERE customer_id = (SELECT customer_id FROM orders WHERE id = 111) ... and both querys are good. But there would be like 20 more rows to go but it just stops... crazy.... – Michael Nov 07 '14 at 07:20

1 Answers1

0

Ok Guys,

so solution is pretty simple.

Just close the MySqlDataReader first -.-... lol

sda.Close();

after the while loop.

Cheers,