0

I am trying to add some session values and if any of the value have a decimal value ie 12.50 I get an error that Input string was not in a correct format.?

Dim Res As Integer = Convert.ToInt32(Session("ConsultingFeeAmount")) + Convert.ToInt32(Session("FoodAndBeverageAmount"))
    TotalAmount = Environment.NewLine + "Total Amount: " + Session("ConsultingFeeAmount") + Session("FoodAndBeverageAmount")
    TotalAmount = "Total Amount " + Res.ToString
user1342164
  • 1,434
  • 13
  • 44
  • 83

4 Answers4

0

I think your problem lies in the code Convert.ToInt32. You can't pass in a decimal number there. It's expecting an integer. That occurs more than once in your code.

DOK
  • 32,337
  • 7
  • 60
  • 92
  • What other options could I try? – user1342164 Jun 28 '13 at 19:02
  • How about Convert.ToDecimal? – DOK Jun 28 '13 at 19:11
  • Conversion from string " Total Amount: 12.5013.00" to type 'Double' is not valid. – user1342164 Jun 28 '13 at 19:16
  • You have defined Res as an integer. And you are casting the session variables as integers when it appears that at least one of them may contain a decimal value. It appears that Res and the session variables should all be decimals (or doubles). – DOK Jun 28 '13 at 19:20
0

I am guessing the value you passed to Convert.ToInt32 are not a valid numeric ones. Make sure you check the session values are empty or not before using that.

if Session("ConsultingFeeAmount") IsNot Nothing Then
  ' Now use this session variable
End If
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

Use a TryParse method from the Decimal class

Dim consultAmt As Decimal
Dim foodAmt As Decimal
Decimal.TryParse(Session("ConsultingFeeAmount"), consultAmt))
Decimal.TryParse(Session("FoodAndBeverageAmount"), foodAmt))

Dim Res As Decimal = consultAmt + foodAmt
TotalAmount = Environment.NewLine & "Total Amount: " & _ 
              consultAmt.ToString() & " " & foodAmt.ToString()
TotalAmount = "Total Amount " & Res.ToString

The Decimal.TryParse analize the input string and set the second parameter with the converted value if it is possible to convert the string to a decimal. If not the method doesn't rises any exceptions and the second parameter is let alone to its default value.

EDIT The OP says that after the change initially suggested now it has an error message that says:

Conversion from string " Total Amount: 12.50 13.00" to type 'Double' is not valid

The problem was the + operator used to concatenate strings when Option Strict is OFF. In that case the VB compiler confuses the meaning of the operator and tries to sum two numeric values. I really suggest to use Option Strict On and Option Explicit On because this will force your code to be more precise (no implicit conversion of types). Of course if you make this change you need an extensive retest of your application You can read about this problem in this question/answer link

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • When I try that I get Conversion from string " Total Amount: 12.50 13.00" to type 'Double' is not valid. – user1342164 Jun 28 '13 at 19:13
  • Double? I use decimals not doubles. Could you explain? – Steve Jun 28 '13 at 19:15
  • Are you sure that TotalAmount is declared as `Dim TotalAmount as String` – Steve Jun 28 '13 at 19:20
  • Yes, maybe theres a different way to get the totals – user1342164 Jun 28 '13 at 19:22
  • 1
    Changed to use the & operator to concatenate the two values. Could you try now? – Steve Jun 28 '13 at 19:25
  • The problem was the + operator used to concatenate strings when Option Strict is OFF. In that case the VB compiler confuses the meaning of the operator and tries to sum two numeric values. I really suggest to use Option Strict On and Option Explicit On because this will force your code to be more precise (no implicit conversion of types). Of course if you make this change you need an extensive retest of your application – Steve Jun 28 '13 at 19:50
0

I don't know if vb.net is different than C#, but Session returns an object not a typed value. You will need to cast Session("ConsultingFeeAmount") to a decimal.

CType(Session("ConsultingFeeAmount"), Decimal)

or

CType(Session("ConsultingFeeAmount"), Integer)
andleer
  • 22,388
  • 8
  • 62
  • 82