Input string was not in a correct format.

bkedersha

Newcomer
Joined
Aug 2, 2005
Messages
13
I have a new error on my page. I enter in a new grant, it does not have any budget data. When I click on the tab, this new error comes up. In the application, the user enters in a new Grant. There is in Budget data for the Grant. When a user selects the Budget tab, the page tries to retrieve the Grant's Budget data, but there is none in the table. The previous developer did not understand that with ASP.NET, you cannot get an empty record set.


Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 42: conn.close()
Line 43: lblCurrencyName.text = session("CurrencyName")
Line 44: select case GrandTotalUSDValue.text > 250000
Line 45: case true
Line 46: 'GrandTotalUSDValue.cssclass="ColorUp"


Source File: D:\GrantManagementWeb\BudgetDataForm.aspx Line: 44

Stack Trace:


[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat) +195
Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value, NumberFormatInfo NumberFormat) +84

[InvalidCastException: Cast from string "" to type 'Double' is not valid.]
Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value, NumberFormatInfo NumberFormat) +173
Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value) +7
ASP.BudgetDataForm_aspx.Page_Load(Object Sender, EventArgs e) in D:\GrantManagementWeb\BudgetDataForm.aspx:44
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750


:confused:
 
Last edited:
Cast from string "" to type 'Double' is not valid

Looks like GrandTotalUSDValue.text is blank. It needs to be a double for your Select Case comparison
 
GrandTotalUSDValue.text is blank because it has no values until a udget is entered

GrandTotalUSDValue.text is blank. The user needs to enter in the first budget totals before the text box comes back with a value.


Jackpanel said:
Cast from string "" to type 'Double' is not valid

Looks like GrandTotalUSDValue.text is blank. It needs to be a double for your Select Case comparison
:confused:
 
If your textbox is blank, you're going to get an error with this line:

select case GrandTotalUSDValue.text > 250000

Its trying to compare "" to 250000 - hence the Cannot Convert "" to Double error. You need to first make sure there is a number in the Grand Total box before checking to see whether its higher than 250000.

Try wrapping the Select statement in a :

Code:
If IsNumeric(GrandTotalUSDValue.text) Then
     Select Case GrandTotalUSDValue.text > 250000
          case true
                 'GrandTotalUSDValue.cssclass="ColorUp"
                 ' other processing if necessary
     End Select
End If
 
Actually, the error is occurring because you cannot compare a string and a double (or integer, or any other numeric data type). You could be comparing "moo" > 2500 or "1337" > 2500... obviously it won't work.

This is where the static methods of the System.Convert class come into play:

Visual Basic:
If GrandTotalUSDValue.Text.Trim().Length > 0 Then ' Check for a value first
  Try
    select case Convert.ToDouble(GrandTotalUSDValue.text) > 250000 ' Convert to double first
      case true
        'GrandTotalUSDValue.cssclass="ColorUp"
      ' Other cases go here
    End Select
  Catch ex as InvalidCastException ' Catch any errors
    ' Display an error message
  End Try
Else
  ' No value entered
End If

I'd also reccomend reading MSDN on Developing a Validator Control
 
Back
Top