Pergunta

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?

Foi útil?

Solução

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

Outras dicas

Just use the = operator:

"TrackID = "

That's what you really want anyway.

Use single Quotes and '%' as

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

Hope it Helps..

Refer

RowFilter

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top