System.FormatException

mike55

Well-known member
Joined
Mar 26, 2004
Messages
726
Location
Ireland
Hi,

I am encountering the following error:
System.InvalidCastException: Cast from string "SELECT Email_Add FROM Login wher" to type Double is not valid. ---> System.FormatException: Input string was not in a correct format.
at Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value, NumberFormatInfo NumberFormat)
--- End of inner exception stack trace ---
at Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value)
at MsgWebService.moduleMessage.getSendersEmail(Int16 senderID) in c:\inetpub\wwwroot\MsgWebService\myModules\moduleMessage.vb:line 254
at MsgWebService.moduleMessage.sendMessage(DataRow messageDetails) in c:\inetpub\wwwroot\MsgWebService\myModules\moduleMessage.vb:line 133
at MsgWebService.wsSendMessages.sendMessage() in c:\inetpub\wwwroot\MsgWebService\wsSendMessages.asmx.vb:line 94
at MsgWebService.wsSendMessages.startProcessing() in c:\inetpub\wwwroot\MsgWebService\wsSendMessages.asmx.vb:line 57

The code that I am using is:
Code:
 localConnection()   Open a connection to the database.

        Set-up the command
        sqlCommand.Connection = conSQL
        sqlCommand.CommandType = CommandType.Text
        sqlCommand.CommandText = "SELECT Email_Add FROM Login where Group_HeadID = " + senderID

        Try
            getSendersEmail = sqlCommand.ExecuteScalar  Execute the statement
            Return getSendersEmail.ToString
        Catch
        Finally
            conSQL.Close()     Close the database connection.
        End Try

From what I can see, my SQL statement is correct, and the connection is being opened correctly. The problem seems to occure when I try and assign the sql statement to the sqlconnection. The sqlconnection is defined as a new sqlconnection, and as a global variable.

Any suggestions??

Mike55
 
Last edited by a moderator:
Are you using option strict? (If you arent, you probably should be.)

Try changing
[VB]
sqlCommand.CommandText = "SELECT Email_Add FROM Login where Group_HeadID = " + senderID
[/VB]
To
[VB]
sqlCommand.CommandText = "SELECT Email_Add FROM Login where Group_HeadID = " & senderID.ToString
[/VB]

When joining strings, & is usually better than + because + might be interpreted as addition rather than concatenation.
 
mike55 said:
sqlCommand.CommandText = "SELECT Email_Add FROM Login where Group_HeadID = " + senderID
Youre trying to add a string to a double. Thats what the first line of the error message tells you (or at least me ;))

Cast your senderID to a string and youll be fine.
Code:
sqlCommand.CommandText = "SELECT Email_Add FROM Login where Group_HeadID = " + senderID[b].tostring[/b]

HTH
/Kejpa
 
Back
Top