Hello everyone


I have a problem using a "search" statement in mysql, using c#, here is my code:

private void btnBuscar_Click(object sender, EventArgs e)
        {
            SearchBook(txtBuscar.Text);
        }

 private void SearchBook(string bookID)
        {
            String connStr = "Server=localhost; Port=3306; Database=biblioteca;Uid=root;Pwd= -----;";
            DataTable dt = new DataTable();

            const string query = "SELECT FROM libros WHERE l_LibroID = @l_LibroID";
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand(query, conn))
                {
                    cmd.Parameters.Add(new MySqlParameter("@l_LibroID", MySqlDbType.VarChar)).Value = bookID;
                    cmd.ExecuteNonQuery();
                    dt.Load(cmd.ExecuteReader());
                  
                    dgvResultadosBusqueda.DataSource = dt;
                }
            }
            
        }

My problem here is that I want to display some data into a datagridview but I have an exception, so, if you can help me to find what I'm a doing wrong, I would appreciate it :),

Thanks in advance

Well, I've solved this problem, thank you ...

Here's my working code if you need it.

private void SearchBook(string bookID)
        {
            String connStr = "Server=localhost; Port=3306; Database=biblioteca;Uid=root;Pwd=dark;";
            DataTable dt = new DataTable();

            const string query = "SELECT * FROM libros WHERE l_LibroID = @l_LibroID";
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand(query, conn))
                {
                    cmd.Parameters.Add(new MySqlParameter("@l_LibroID", MySqlDbType.VarChar)).Value = bookID;
                    
                    try
                    {
                        dt.Load(cmd.ExecuteReader());
                        dgvResultadosBusqueda.DataSource = dt;
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("ERROR");
                    }
                    finally
                    {
                        conn.Close();
                    } 
                }
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.