skip to Main Content

Hello if i execute the following query in PhpMyAdmin i have 5 Result.
If i execute the exactly query into C# i get back only 1 Result.
I have been beating my head against the table for 2 days but I can’t find the solution.
Some ideas?

SELECT 
tbl_orders.Data_payment,
tbl_customers.City,
tbl_customers.Country,
tbl_orders.Price,
tbl_orders.state_order,
tbl_products.Product_ID,
tbl_products.Name
FROM
tbl_orders
    INNER JOIN
tbl_customers ON tbl_orders.fk_customer_id = tbl_customers.Customer_ID
    INNER JOIN
tbl_products ON tbl_orders.fk_product_id = tbl_products.Product_ID
WHERE
tbl_products.Product_ID = '2'
    AND tbl_customers.Country = 'Russia'
    AND tbl_orders.Data_payment BETWEEN '2019-04-01' AND '2020-04-01'



        private void test_load(string query)
    { DataTable dt = new DataTable();
        try
        {
            using (MySqlConnection conn = new MySqlConnection(MyConString))
            {
                using (MySqlCommand cmd = new MySqlCommand(query, conn))
                {
                    conn.Open();
                    using (MySqlDataReader dr = cmd.ExecuteReader())    
                    {               
                        dt.Load(dr);
                        MessageBox.Show(dt.Rows.Count.ToString());
                        dataGridView1.DataSource = dt;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("err test_load " + ex);
        }
    }

2

Answers


  1. Chosen as BEST ANSWER

    Ok so i finally solve the problem.

    This pice of code in not working it return only one row and should return more than one.

            private DataTable test2_load(string query)
        {
            DataTable dt = new DataTable();
            try
            {
                using (MySqlConnection conn = new MySqlConnection(MyConString))
                {
                    using (MySqlCommand cmd = new MySqlCommand(query, conn))
                    {
                        conn.Open();
                        using (MySqlDataReader data_reader = cmd.ExecuteReader())
                        {                     
                            dt.Load(data_reader);
                        }
                    }               
                }
                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show("err test_load " + ex);
                return null;
            }
        }
    

    This is working. It return the right number of rows

            private void test3_load(string query)
        {
            MySqlConnection mysqlCon = new
    
             MySqlConnection(MyConString);
            mysqlCon.Open();
    
            MySqlDataAdapter MyDA = new MySqlDataAdapter();
    
            MyDA.SelectCommand = new MySqlCommand(query, mysqlCon);
    
            DataTable table = new DataTable();
            MyDA.Fill(table);
    
            BindingSource bSource = new BindingSource();
            bSource.DataSource = table;
    
            dataGridView1.DataSource = bSource;
        }
    

  2. After adding the database and testing the code you provided, I find that I still

    can not reproduce your problem. I can get the same result in sql-query and in c# code.

    You can see the following picture.

    enter image description here

    I also tested it, I find it is different from your picture.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search