Reputation: 23
I try to get the quantity with a SQL query but it is not working;
string selectrow;
int rowindex = dataGridView1.CurrentCell.RowIndex;
int columnindex = dataGridView1.CurrentCell.ColumnIndex;
selectrow = dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString();
string query = @"select quantite_pdt from produits where ref_pdt = @pp";
try
{
using (var conn = loaddatabaseconnexion.connexion_BDD())
using (var cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@pp", int.Parse(selectrow));
SqlCommand command = new SqlCommand(query, loaddatabaseconnexion.connexion_BDD());
string nbstr = command.ExecuteScalar().ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
I don't understand because my variable @pp is been declared.
Upvotes: 1
Views: 82
Reputation: 33581
You should be using a single command object here instead of two. Your code should look like this. I would suggest not using AddWithValue. It can get datatypes incorrect sometimes. http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
using (var cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@pp", int.Parse(selectrow));
string nbstr = cmd.ExecuteScalar().ToString();
}
Upvotes: 4