3

I have Sql table Tracks which keeps track information such as TrackID and TrackName. I have c# application with textbox and listbox. I use dataSet to retrieve sql table and when I write "3" on textbox, I want to get track names on listbox which has TrackID as "3". My code follows:

lbxTracks.DataSource = new DataView(das.Tables[0], "TrackID LIKE " + idNo.ToString(), "TrackName", DataViewRowState.CurrentRows);

But I get an error as LIKE function cannot be used on System.Int32..

Any ideas?

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
Sertan Pekel
  • 593
  • 2
  • 5
  • 23

3 Answers3

4

I don't know any other way to include something like ToString() method in a Expression string for RowFilter but this way it works:

lbxTracks.DataSource = new DataView(das.Tables[0], "TrackID + '' LIKE '" + idNo + "'", "TrackName", DataViewRowState.CurrentRows);

NOTE the + '' this will turn your TrackID into string before performing the LIKE. And it works like a charm. If anyone knows another way to perform some ToString() on a column in the RowFilter string, please leave comment below. I'm really appreciated for that :)

You should use string.Format() to concatenate string, and notice about the use of % in LIKE expression. I think in this case you may want to use the right operator (= is OK for number) if you just want to compare the equality in value (not format).

King King
  • 61,710
  • 16
  • 105
  • 130
2

Just use the = operator:

"TrackID = "

That's what you really want anyway.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • This in fact answers to the **specific** problem of the OP, however it doesn't answer to the general problem, for example what if the OP wants to filter for `TrackID` with an integer having format `11?22?33`, so all the values like `11122133`, `11222133`,`11322033`, ... (there are many) should be included. So the solution is somehow we have to convert the `int` to `string` and perform the LIKE on that. – King King Aug 21 '13 at 17:45
  • @KingKing, your example is much more abstract than the OP's, and thus theoretical. I'm confident we could find a way, but as you stated, in this case we don't need to so the OP just needs to use the right operator, yes? – Mike Perrenoud Aug 21 '13 at 17:48
0

Use single Quotes and '%' as

lbxTracks.DataSource = new DataView(das.Tables[0], 
                       "TrackID LIKE '%" + idNo + "%'", 
                       "TrackName", DataViewRowState.CurrentRows);

Hope it Helps..

Refer

RowFilter

Gokul E
  • 1,356
  • 2
  • 13
  • 28