Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataView LIKE function on Integer value

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?

like image 332
Sertan Pekel Avatar asked May 03 '26 13:05

Sertan Pekel


2 Answers

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).

like image 156
King King Avatar answered May 05 '26 02:05

King King


Just use the = operator:

"TrackID = "

That's what you really want anyway.

like image 27
Mike Perrenoud Avatar answered May 05 '26 02:05

Mike Perrenoud