# Thread: calculate from data table

1. ## calculate from data table

'i have a tblFreightdataset with the following Coloum and row
ID FreightType Value FrightValue
1 CIF 0.00 []
2 FOB 0.015 []
3 CFR 0.015 []

manual calculation from a button Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CIF As Double = 0
Dim FOB As Double = 0.015
Dim CFR As Double = 0.015
Dim Cost As Double = txbCostValue.Text
Dim Freight As Double = txbFreightAmount.Text
If cmbFreightType.Text = "CIF" Then txbInsureanceAmount.Text = (Cost + Freight) * CIF
ElseIf cmbFreightType.Text = "FOB" Then txbInsureanceAmount.Text = (Cost + Freight) * FOB
ElseIf cmbFreightType.Text = "CFR" Then
IF cmbFreightType.Text= "CFR" Then
txbFreightAmount.Text= 0
End If
txbInsureanceAmount.Text = (Cost + Freight) * CFR

End If
End Sub

the code above work as it should but that the manual way
i want to be able to select from cmbFreightType and it search my table for the matching string and calculate as above my table has a check box if check then my txbFreightAmount = 0
my forum is already bind to the dataset in the program so hence i dont need to do a conection

2. ## Re: calculate from data table

if check then my txbFreightAmount = 0
Huh? Why do you need that? Can't you just record a zero and have done with it? Not a lot makes any sense here. How can the value of 'CIF' be 0.00? Nothing can be carried free, can it? And if it can, what's the purpose of ...

If cmbFreightType.Text = "CIF" Then txbInsureanceAmount.Text = (Cost + Freight) * CIF ?

This can only ever have the value 0 so why calculate it at all? And what's

ElseIf cmbFreightType.Text = "CFR" Then
IF cmbFreightType.Text= "CFR" Then
txbFreightAmount.Text= 0

... do for you? You appear to be charging for a non-existent load!

3. ## Re: calculate from data table

Wow. There were rumors that dunfiddlin was back, but now it's official.

One thing I would say about the code, in addition to what dun already said, is that you should really turn Option Strict ON for the project and fix the errors. You are doing some dangerous implicit conversions. Leave a textbox blank, or enter an invalid string, and the code you have will crash when it fails to implicitly convert the contents of the textbox (which HAS to be a string) into a number. To safely and correctly convert data from a textbox to a number you should be using Double.TryParse. If you could be certain that the string was in good shape, you could use CDbl, but when it's user input, you can never trust the user....even if it's just you.

Another point is that when using money, the better data type to use is Decimal rather than Double. The Double data type has some little imprecisions that can eventually add up to a penny or two.

4. ## Re: calculate from data table

no the text box wont be left blank it will always have a value the reason it need to be 0 if check in case some one place a value in the check box it will turn it in to 0 as that CFR mean the freight is already in the cost value so that box need to be 0 do u understand?

5. ## Re: calculate from data table

i am in to shipping of cars and there are ways and mean of transporting them. if i am going by sea i use CIF, CFR, FOB
CIF mean i bought the with freight and insurance into the price. so my insurance and freight box need to be 0
CFR mean i bought the car with freight but no insurance. so my freight box need to be 0 and my insurance box will be 0.015 * cost box
FOB mean i bought the car with no insurance or freight so i have to put in a freight value manually and it is added to the cost then * the insurance 0.015

6. ## Re: calculate from data table

do u understand?
Er .. is that rhetorical?

7. ## Re: calculate from data table

Originally Posted by Dwaynewayne
i am in to shipping of cars and there are ways and mean of transporting them. if i am going by sea i use CIF, CFR, FOB
CIF mean i bought the with freight and insurance into the price. so my insurance and freight box need to be 0
CFR mean i bought the car with freight but no insurance. so my freight box need to be 0 and my insurance box will be 0.015 * cost box
FOB mean i bought the car with no insurance or freight so i have to put in a freight value manually and it is added to the cost then * the insurance 0.015
Ok that makes a little more sense but it's hard to see just how that translates to the datatable that you originally proposed. There is no mystery to datatable calculations. You simply add a new DataColumn to the table and set its Expression property. See the MSDN blurb here. If you've any familiarity with spreadsheets you should have few problems applying the principles although the datatable expression is slightly more limited (you can only refer to values in the same datatrow, for example).

8. ## Re: calculate from data table

yes that is what i want just the value in the row what i dont want to so is every time i add a new row i have to write a line of code
i am pretty much a newbie to this i am about a week in to programing

9. ## Re: calculate from data table

Shouldn't it be 1 if by land and 2 if by sea?

(it is likely that only Americans will get that)

It doesn't really matter whether the controls should always be filled in or not. With the code you have, if the user makes a mistake the program will crash out. That's not a good thing to do. The reason for this is due to the implicit conversions you are using. Option Strict ON prevents you from making mistakes like that. Additionally, explicit conversions are faster than implicit conversions, though you'll never see the difference in that code, as it would be a tiny fraction of a millisecond.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured