Reputation: 81
I'm trying to save the unique value resulting from a query into a double variable, since the same is stored in the database as double, so I can later be able to multiply it with a counter, with the following code:
public partial class CoronaClaraCant : Form
{
private int conteoCliks = 0;
private string producto = "CoronaClara";
private double precio;
public CoronaClaraCant()
{
InitializeComponent();
}
private void CoronaClara_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("server=localhost;database=database;username=root;password=***");
conn.Open();
string select = "SELECT precio FROM productos where prodnom = '" + producto + "';";
MySqlCommand cmd = new MySqlCommand(select, conn);
double result = cmd.ExecuteNonQuery();
conteoCliks++;
precio = result;
double total = (precio * conteoCliks);
lblcantidad.Text = conteoCliks.ToString();
lblprecio.Text = precio.ToString();
lbltotal.Text = total.ToString();
}
}
So far I've only been getting negative values:
I'm guessing this could be because the ExecuteNonQuery, but I haven't been able to assign a double value to a ExecuteScalar method, and I'm not sure if there are other parameters I should consider when saving a double value into a variable.
Upvotes: 0
Views: 190
Reputation: 14614
You want to select
data from database and you are calling ExecuteNonQuery
which is used for Insert,Update or Delete commands.
You need to call ExecuteReader
or if you want to get data in a DataTable
you can fill a DataTable
.
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
if (rdr.Read())
{
precio = (double)rdr["precio"];
}
}
Upvotes: 1