Problem Selecting from Datatable

trevorjdaniel

Member
Joined
Sep 24, 2009
Messages
8
Programming Experience
1-3
Hi,

I am having a problem selecting records from a databable.

This is what I am trying to achieve...

If I have a list of say..

banana
apple
orange
orange2

and people type in "a" - I want it to display all records. When they then type in "an", I want it to display banana, orange and orange2 but not apple

I thought I could do this with the "like" statement with %s either side but i wont work for me..

the code i am using is this

VB.NET:
Dim drs() As DataRow = dt.Select("name like '%" & strText & "%'")

When i run the above code and put in "a" I only get apple at the moment.

Can anyone help please?

Thanks
Trev
 
The code you have works exactly as it should. If you're not getting the results you expect then there's either something wrong with your data or your input. Are you sure you're using the correct value for strText?
VB.NET:
Dim table As New DataTable

table.Columns.Add("Name", GetType(String))

With table.Rows
    .Add("banana")
    .Add("apple")
    .Add("orange")
    .Add("orange2")
End With

Dim text As String = "a"
Dim rows As DataRow() = table.Select("Name LIKE '%" & text & "%'")

MessageBox.Show(rows.Length.ToString())

text = "an"
rows = table.Select("Name LIKE '%" & text & "%'")

MessageBox.Show(rows.Length.ToString())
 

Latest posts

Back
Top