I currently have a DataGridView which displays all the item. I would like to sum all the prices in the price column and then reflect the total in a label, ‘TotalValueLabel’. What’s wrong with my statement?
string query = "SELECT SUM (Price) FROM Bill";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, DBconn);
DataTable source = new DataTable();
dAdapter.Fill(source);
TotalValueLabel.Text = source.ToString();
Your source is a DataTable so “
source.ToString()” will not give you your result,Try “
source.Rows[0][0].ToString();“.DataTable object contains a list of DataRow objects which hold values for each row of your query result.
In your case however you might not need this. If you are looking for a single value you should use IDbCommand and call
ExecuteScalar(). This will return the first value of the first row of your results.Also try calling
Dispose()on objects that implement IDisposable (like dbadapter, command, connection).