Show search from textbox to listview from a stored procedure MySQl

1

I'm starting with databases (MySQL) with C # and I'm using ListView and DataGrid to show the table. I have a problem when I do the search by completing TextBox and clicking on the button1 button to fill the ListView (I invoke the bd table through DataTable ), causes the following error

  

System.ArgumentException: 'Parameter' searchValue 'not found in the   collection. '

Which the reader has a null value.

The truth is that I am half lost and I can not find a way to do it.

Here the code:

The table is called productos and has 3 columns: idproductos int , nombre_producto varchar and precio_producto int .

Stored Procedure :

    CREATE DEFINER='root'@'localhost' PROCEDURE 'BusquedaProductoPorValor'(
buscarValor varchar (50)
)
BEGIN
select *
from productos
where nombre_producto like concat('%',buscarValor,'%');
END

C #

private void button1_Click_1(object sender, EventArgs e)
        {

            using (MySqlConnection mysqlcon = new MySqlConnection(connectionString))
            {

                mysqlcon.Open();

                MySqlDataAdapter SqlLista = new MySqlDataAdapter("BusquedaProductoPorValor", mysqlcon);
                SqlLista.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlLista.SelectCommand.Parameters.AddWithValue("buscarValor", Buscartxt.Text);

                DataTable ListaNueva = new DataTable();

                SqlLista.Fill(ListaNueva);


                dataGridView1.DataSource = ListaNueva;

                MySqlCommand mySqlCmd = new MySqlCommand("BusquedaProductoPorValor", mysqlcon);
                mySqlCmd.CommandType = CommandType.StoredProcedure;


                MySqlDataReader Reader = mySqlCmd.ExecuteReader();

                listView1.Items.Clear();

                while (Reader.Read())
                {
                    ListViewItem lv = new ListViewItem(Reader.GetInt32(0).ToString());
                    lv.SubItems.Add(Reader.GetString(1));
                    lv.SubItems.Add(Reader.GetString(2));
                    listView1.Items.Add(lv);

                }
                Reader.Close();






        }
            LimpirarCampos();

        }
    
asked by Agus Veneziano 01.09.2018 в 22:24
source

1 answer

1

The problem is in this part of the code:

MySqlCommand mySqlCmd = new MySqlCommand("BusquedaProductoPorValor", mysqlcon);
mySqlCmd.CommandType = CommandType.StoredProcedure;
MySqlDataReader Reader = mySqlCmd.ExecuteReader();

You still have to add the necessary parameter to call the stored procedure:

SqlLista.SelectCommand.Parameters.AddWithValue("buscarValor", Buscartxt.Text);
    
answered by 06.09.2018 в 16:11