Cannot perform = operation on string and Int32

nkotbox

Active member
Joined
May 28, 2011
Messages
31
Programming Experience
Beginner
I have developed this database frontend and am having this issue sometimes.
When I search for a serial # in my DB, sometimes I get the error "Cannot perform = operation on system.string and int32
What I cannot understand is this works for many serial #'s and does not work for others. Below is my code, is there a better way of doing this?

VB.NET:
        Dim foundrows() As DataRow
        Dim thatserial = CInt(SerialTB.Text)
        Dim SerialSql As String = "[SerialNO] = " & (thatserial)
        'SerialSql = SerialSql & thatserial
        Dim SortDate As String = "dt"


        foundrows = dsCallLog.Tables("Call Log Sub").Select(SerialSql, SortDate)

I have tried taking out the cast to an integer but it did not work. I have wrapped the serial # in single quotes but then it never finds any records. Even records that currently work are not found when wrapped in single quotes. Is there a way to parameterize this so that it works better?
 
The problem you're getting is down to how you build your SerialSql string...

VB.NET:
Dim SerialSql As String = "[SerialNO] = " & (thatserial)

If your serial numbers are VARCHAR's in the DB, then I'm pretty sure you'll need to enclose the thatserial var in quotes to denote a string.

VB.NET:
Dim SerialSql As String = "[SerialNO] = '" & thatserial.ToString() & "'"
 
In my post I stated that "I have wrapped the serial # in single quotes but then it never finds any records" . My try was a little different than yours but had the same output. I tried your suggestion just to be safe and had the same outcome. My first try looked like this:
VB.NET:
Dim SerialSql As String = "[SerialNO] = " & "'" & (thatserial) & "'"

When I try this I get 0 results. I get 0 results for any serial # where currently the code works on most but no all serials. I do not see any difference in the DB to cause an issue. I went as far as to replace a line in the DB. I took a serial # that was working and replaced it with a serial # that does not work and it still didnt work. I thought that maybe some other field associated with the serial # was causing the issue.

In the DB, the SerialNO field is a text field.

If you have any other idea's Please let me know...:confused:
 
My bad - should have spotted you'd tried the quotes.

Have you tried dropping a Try...Catch block around the offending code? If not, then it'd be a good point to stick a breakpoint at the Catch point and take a look at what's going on when the exception gets raised.

Do the textbox entered serial number's ever start with zero? Just a thought, given that casting to an integer will drop any leading zeroes.
 
All of the serial #'s start with 0. Oddly enough the serial #'s are found using the leading 0 or not. I had a breakpoint in the code where the error would happen and when I searched serial #'s that would work versus ones that would not work, I did not see any difference. Unexplainable to me.

However, When i deleted all calls associated with a non working serial #, that particular serial # can be called up and it works fine. I placed a few records against that serial # and no issues. It's as if there was some issue with the call records for that serial #. I looked at the data in the DB and did not see anything I thought was strange.

I have since fixed any serial # that was an issue and am moving on.

Thank you for the prompt replies as you helped me keep my sanity while working on this.
 
Glad to hear you've got it working - no problem on the replies... keeps the grey matter ticking over
 
Back
Top