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?
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).
Just use the = operator:
"TrackID = "
That's what you really want anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With