Click here to Skip to main content
14,695,499 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all of you,
I'm working on a small project and I have a small problem :)
I have a database with 6 columns. When I load Form1, I want to see calculate total amount in Label1 from Column "TotalPrice".

Is this possible?
Thank you.

I use Visual Basic 2010
Posted

Yes this is possible :

Use the following SQL statement :
SELECT Sum(TotalPrice) FROM TableName


This will return a single value containing the sum of all values in the TotalPrice columns.

Good luck!
Eduard
   
Comments
Albin Abel 8-Apr-11 10:03am
   
Moved OPs comment here.
Hi Eduard,
Thank you for your quick answer.
I tried that but I don't have a lot of luck.
I'm new to the programming so please let me know where exactly should I put this code.
Thank you again.
Peter
Eduard Keilholz 8-Apr-11 11:03am
   
Update your question and post the code you have so far///
Do you have the data in the database or do you want to calculate the total based on the values currently in a datagridviewcolumn?

If it's the latter, the next question is where the data is coming from? If it's for example from a DataTable, then you could use Compute[^] method.
   
Comments
Member 7821765 8-Apr-11 11:07am
   
I have data in Access database.
For example,
When I load Form1 there is no data in the database.
When I click AddNew Button, some info is saved in the database.
One of the columns is TotalPrice.
I want to be able to see in Label1 the amount of all of the prices combine from column TotalPrice.
When I Delete some of the data from the database, the amount to be automatically deduct from Label1.
Wendelius 8-Apr-11 11:14am
   
Ok, in that case, if you use OleDB connection, the fastest way is to use ExecuteScalar method. See: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executescalar.aspx. There's actually almost complete example for you to use (select total amount of regions). You can use this every time a modification is made.

However, keep in mind that if there are multiple users modifying the data, then you wont see new results immediately when somebody else makes a modification. You'll get the new results (the sum of total price) when you make modifications in the application you use.
Member 7821765 8-Apr-11 13:03pm
   
Thanks for the info,
but I can't make it work.
As I said I'm new to this world.
Thanks again.
Wendelius 8-Apr-11 13:34pm
   
If you tried the example behind the link I provided and it doesn't work, update the post and include the problem you have. Otherwise we're just guessing what your problem could be.
Member 7821765 8-Apr-11 14:13pm
   
Ok Mika,
Thanks.
Wendelius 8-Apr-11 14:22pm
   
No problem. And if you get stuck, you can open a new question and include the code you've done so far. I'll bet you'll get lots of quality answers. Good luck :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900