0

I am using the following code:

string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name  FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bindsrc = new BindingSource();
DataGridView datagridv = new DataGridView();
bindsrc.DataSource = dt;
datagridv.DataSource = bindsrc;
connection.Close();

I have three predefined columns in datagridview1 say 'PLU_Code', 'Barcode' and 'Product_Name'.

I want to display the result (more than one row) of the select query in the datagridview. But I am not getting any.

How do I achieve this?

Phoenix
  • 1,045
  • 1
  • 14
  • 22
Ameena
  • 187
  • 2
  • 5
  • 18
  • Off topic, but: your code is vulnerable for SQL injection. – Stefan Feb 02 '15 at 12:28
  • Problem is when i click on the button there is no rows displayed in datagridview – Ameena Feb 02 '15 at 12:28
  • are you sure you are getting some data when you do adpter.Fille(dt)? – fabricio Feb 02 '15 at 12:28
  • @Stefan You mean the flow is wrong>/ – Ameena Feb 02 '15 at 12:30
  • there are no data displayed @Fabricio – Ameena Feb 02 '15 at 12:30
  • @Ameena: no, check this answer for details: inserting your textbox data directly in your SQL statement is vulnerable for hackers. http://stackoverflow.com/questions/7505808/using-parameters-in-sql-statements – Stefan Feb 02 '15 at 12:33
  • yea, I know that is not data displayed. What I meant to ask was if you know if the result DataTable has some rows in it. If you are getting data from your query. – fabricio Feb 02 '15 at 12:33
  • yes the result of the query is two rows. but i dont think i am getting data in adapter.fill(dt). am i missing something in the code?! – Ameena Feb 02 '15 at 12:41

3 Answers3

0

Try this code:

                string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name  FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
                connection.Open();

                MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
                MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
                DataTable table = new DataTable();
                dataAdapter.Fill(table);

                datagridview1.DataSource = table;

                connection.Close();
user4340666
  • 1,453
  • 15
  • 36
0

Try this code:

string getinputs = "Select Query";
connection.Open();

MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(getinputs,connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmdbuilder);

DataTable dt = new DataTable();
adapter.Fill(dt);

DataGridView datagridv = new DataGridView();
datagridv.DataSource = dt;

datagridv.DataBind();

connection.Close();
Stefan
  • 17,448
  • 11
  • 60
  • 79
vignesh
  • 97
  • 5
0

I got the result. I achieved two empty rows on using @user4340666 code. when i scrolled the grid i found the set of data's been added as new columns leaving predefined column cells empty. so i just cleared the columns.

            dataGridView1.Columns.Clear();
            string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name  FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
            connection.Open();

            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
            MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
            DataTable table = new DataTable();
            dataAdapter.Fill(table);

            datagridview1.DataSource = table;

            connection.Close();
Ameena
  • 187
  • 2
  • 5
  • 18