Input string was not in a correct format - Vista only?

fimble

New member
Joined
Apr 17, 2009
Messages
3
Programming Experience
1-3
I am getting this error when I debug my application on my Vista box at home. I do not get it when running on my XP box at work. I know the error is indicative of converting strings to integers but I can't see where I am going wrong, much less why it only afflicts my Vista machine! I am using Visual Basic 2008 Express on both computers.

Anyone have any ideas, has had me stumped for a couple days now :(

Here is some code:

..........

VB.NET:
Dim Count As Integer = 0
 
        Try
            Count = DataAccess.ReturnTotalSearchRowCount("")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
 
        lbl_TotalSongCount.Text = "Library contains " & Convert.ToString(Count) & " total records"

......

Here is the function being called:

VB.NET:
    Public Shared Function ReturnTotalSearchRowCount(ByVal SearchCrit As String) As Integer
        Dim conn As New SqlServerCe.SqlCeConnection("Data Source = Database1.sdf")
 
        '''Stop
 
        Dim result As Integer = 0
 
        Dim inc As Integer = 0
 
        'Declare and instantiate a DataSet
        Dim ds As New Data.DataSet()
 
        Dim SQL_Com As New SqlServerCe.SqlCeCommand(SQL_Queries.CountSearchResults, conn)
        SQL_Com.Parameters.AddWithValue("CRIT", SearchCrit)
 
        Dim da As New SqlServerCe.SqlCeDataAdapter(SQL_Com)
 
        da.Fill(ds)   <----<----------------------It's failing here
        If ds.Tables(0).Rows.Count > 0 Then
            result = ds.Tables(0).Rows(0).Item(0)
        Else
            result = 0
        End If
 
        conn.Close()
        conn.Dispose()
 
        Return result
    End Function
Here is the SQL statement that is used:

VB.NET:
"SELECT COUNT(ID) FROM Songs WHERE (Artist LIKE '%' + @CRIT + '%') OR (TrackName LIKE '%' + @CRIT + '%') OR (ShortFileName LIKE '%' + @CRIT + '%') OR (Album LIKE '%' + @CRIT + '%')"
 
Ok, it works if I drop the parameter e.g.

VB.NET:
"SELECT COUNT(ID) FROM Songs WHERE (Artist LIKE '%%') OR (TrackName LIKE '%%') OR (ShortFileName LIKE '%%') OR (Album LIKE '%%')"

Or if I keep the param but drop the preceeding and trailing

VB.NET:
'%' +

And

VB.NET:
+ '%'

In other words, this works... (By works I mean doesn't throw the error :))

VB.NET:
"SELECT COUNT(ID) FROM Songs WHERE (Artist LIKE @CRIT) OR (TrackName LIKE @CRIT) OR (ShortFileName LIKE @CRIT) OR (Album LIKE @CRIT)"

What gives??!?
Also I should add, the command works with parameters and all directly in the SQL gui. Of course it asks me to supply the parameter before it actually executes of which I supply an empty string and it works.
 
I resolved this by doing the following:

VB.NET:
    Public Shared Function ReturnTotalSearchRowCount(ByVal SearchCrit As String) As Integer
        Dim conn As New SqlServerCe.SqlCeConnection("Data Source = Database1.sdf")
 
        '''Stop
 
        Dim result As Integer = 0
 
        Dim inc As Integer = 0
 
        'Declare and instantiate a DataSet
        Dim ds As New Data.DataSet()
 
        Dim SQL_Com As New SqlServerCe.SqlCeCommand(SQL_Queries.CountSearchResults, conn)
        
[FONT="Arial"][COLOR="DarkRed"][B]       
            Dim param As New SqlServerCe.SqlCeParameter
            param.SqlDbType = SqlDbType.NVarChar   <---- This was key
            param.Value = SearchCrit
            param.ParameterName = "crit"
            SQL_Com.Parameters.Add(param)
[/B][/FONT][/COLOR]
 
        Dim da As New SqlServerCe.SqlCeDataAdapter(SQL_Com)
 
        da.Fill(ds) 
        If ds.Tables(0).Rows.Count > 0 Then
            result = ds.Tables(0).Rows(0).Item(0)
        Else
            result = 0
        End If
 
        conn.Close()
        conn.Dispose()
 
        Return result
    End Function

Specifying the SqlDbType for the parameter was the key. It still doesn't make much sense to me but I guess results be results /shrug
 
Back
Top