I'm working on a project where I need to use datagridview in c# I want to do a test, when I do the search for a product by it's number, if the number is incorrect it will display an error, which works fine in my case, and if the number is correct it will display the information related to this product in the datagridview, which doesn't work, it gives me nothing when I type a correct number. here is the code, help me please
private MySqlDataAdapter mySqlDataAdapter;
private void button1_Click(object sender, EventArgs e)
{
int n = Convert.ToInt32(t_ref.Text);
string cs = "datasource=localhost;port=3306;database=stock;username=root;password=;";
MySqlConnection con = new MySqlConnection(cs);
try
{
con.Open();
}
catch (Exception)
{
MessageBox.Show("Erreur de connexion à la base de donnée !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
string query = "select * from produits where reference = " + n + "; ";
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
dg2.DataSource =dr;
}
else
{
MessageBox.Show("Aucun élément avec ce reférence a été trouvé !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}