Solved

I error out when I ask my datatable to select rows that are equal to a search argument.

Posted on 2004-08-19
26
361 Views
Last Modified: 2010-05-18
Here is some code

If x = 1 Then
                CurrRow = dt.Select("IDNUM = " & Search, "IDNUM")
            Else
                If x = 2 Then
                    CurrRow = dt.Select("CHECKNUM = " & Search, "CHECKNUM")
                Else
                    If x = 3 Then
                        CurrRow = dt.Select("DATE" Is Search, "DATE")
                    End If

                End If

            End If
The first line is comparing whats in 'search' to the column idnum.  No problems and works perfectly.  The second line that is done exactly the same gives me this error:
Cannot perform '=' operation on System.String and System.Int32
I have tried all kinds of combinations for this and cant figure it out.  The third line happens the same way.  It just shows yet another way that I tried.  The only thing that I can think of is that in the table these columns come from.........IDNUM is defined numeric......Checknum and DATE are defined as text.  Does this have something to do with it?  If it does, how do I do a select statement and compare it to a text defined field? I cannot change the table.
0
Comment
Question by:CooperTupelo
  • 14
  • 11
26 Comments
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11844638
you need to cast the int to a string on the 2nd and 3rd Select's so that the data types being compared are the same.
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11844653
If x = 1 Then
                CurrRow = dt.Select("IDNUM = " & Search, "IDNUM")
            Else
                If x = 2 Then
                    CurrRow = dt.Select("CHECKNUM = " & Search.ToString(), "CHECKNUM")
                Else
                    If x = 3 Then
                        CurrRow = dt.Select("DATE" Is Search.ToString(), "DATE")
                    End If

                End If

            End If
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11844669
What is the DataType of "CHECKNUM" in the table ?
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11844684
"Checknum and DATE are defined as text"
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11844713
I am only assuming Search is a number type, probably int, since IDNUM is numeric and that line works
0
 

Author Comment

by:CooperTupelo
ID: 11845083
Nope......Search is defined as string
I get the same error that way.
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11845121
what is the value of search?
0
 

Author Comment

by:CooperTupelo
ID: 11845160
40-022229
it pulls a value from the previous window.  The value is typed in from the user
it happens to be a check number
I have been testing with a known good check number 40-022229
0
 

Author Comment

by:CooperTupelo
ID: 11854572
Does anyone know??  Please HELP!!
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11854597
I think the problem is related to checknum and date being defined as text.  if you could convert them to varchar that might help.  if not, try doing appropriate casting.  you could try variations of the code to track down exactly what is causing it.  test your variables.  make sure everything's right.
0
 

Author Comment

by:CooperTupelo
ID: 11854693
I know that those two variables being defined as text if the problem.  But I cannot change that and this is coming from an Access table.  How can I do the above code with those two variables defined as text?
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11854824
i just realized you can't use an = opperator on a TEXT field in SQL.  try using LIKE instead

If x = 1 Then
                CurrRow = dt.Select("IDNUM = " & Search, "IDNUM")
            Else
                If x = 2 Then
                    CurrRow = dt.Select("CHECKNUM LIKE " & Search, "CHECKNUM")
                Else
                    If x = 3 Then
                        CurrRow = dt.Select("DATE LIKE " & Search, "DATE")
                    End If

                End If

            End If
0
 

Author Comment

by:CooperTupelo
ID: 11855060
That dont work either.  It gives me this error
Cannot perform 'Like' operation on System.String and System.Int32
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:trevorhartman
ID: 11855080
ok so even though CHECKNUM and DATE are text, .net is storing them as strings.  i don't get the int though.  if search is not an int, where is the int??

also, why were you using Is Search in this line: CurrRow = dt.Select("DATE" Is Search, "DATE") ?
0
 

Author Comment

by:CooperTupelo
ID: 11855127
I was using is just to try something different.  I just posted it that way to show that I tried that also.
IDNUM is defined as a number in the Access table.  CHECKNUM and DATE are both defined as text.  I dont understand it either.  You'd think with Search dimmed as a string that it would be harder to get IDNUM to compare to it then the two text fields.
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11855140
you could try to get any type of Select to work with CHECKNUM

dt.Select("CHECKNUM = 40-022229")

see if you can get anything to work and we'll go from there
0
 

Author Comment

by:CooperTupelo
ID: 11855191
This works
CurrRow = dt.Select("CHECKNUM = '40-022229'", "CHECKNUM")
but this does not
CurrRow = dt.Select("CHECKNUM = " & "40-022229", "CHECKNUM")
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11855213
crazy. how about

Dim strSelect As String = "CHECKNUM = " & "40-022229"
CurrRow = dt.Select(strSelect, "CHECKNUM")
0
 

Author Comment

by:CooperTupelo
ID: 11855241
That gives the same error.
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11855251
and this doesnt?

Dim strSelect As String = "CHECKNUM = 40-022229"
CurrRow = dt.Select(strSelect, "CHECKNUM")
0
 

Author Comment

by:CooperTupelo
ID: 11855287
Nope that works.
0
 

Author Comment

by:CooperTupelo
ID: 11855307
This does work.....I just tried it.
Dim strSelect As String = "CHECKNUM = " & "'40-022229'"
CurrRow = dt.Select(strSelect, "CHECKNUM")

You have to have the apostrophes around the 40-022229
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11855310
something strange is going on.. i dont know what to tell you.  you could try different methods of building your string.  use a StringBuilder.. use seperate variables and try concatenating?
0
 
LVL 8

Accepted Solution

by:
trevorhartman earned 500 total points
ID: 11855327
aah, makes sense now!

it was seeing the leading 4 and automatically treated it as an int.

If x = 1 Then
                CurrRow = dt.Select("IDNUM = " & Search, "IDNUM")
            Else
                If x = 2 Then
                    CurrRow = dt.Select("CHECKNUM = '" & Search & "'", "CHECKNUM")
                Else
                    If x = 3 Then
                        CurrRow = dt.Select("DATE = '" & Search & "', "DATE")
                    End If

                End If

            End If
0
 

Author Comment

by:CooperTupelo
ID: 11855362
YEAH!!! It works!!!!
You tha Man!!
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11855372
glad i could help, sorry it took so long to figure out that tiny little error.  those are the worst
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now