DataTable select method, how to select with a variable?

Hi,

I have a dataTable, and I would like to select a column by using the string come from my Textbox.

 Dim dt As DataTable = dsDataSet.Tables("CustomerTable")
 Dim row As DataRow = dt.Select("Name =  txtMyTextBox.Text")(0)

However it gives me error for the second line as it is a wrong expression.
I would like to know how to do it properly?

I tried:
= dt.Select("Name =  'txtMyTextBox.Text' ")(0)
= dt.Select("Name =  {txtMyTextBox.Text}")(0)
= dt.Select("Name = " & txtMyTextBox.Text)(0)

but I guess its all wrong........

Please could you tell me the correct method? Many thanks.
stainesAsked:
Who is Participating?
 
RonaldBiemansConnect With a Mentor Commented:
oeps, this is correct

Dim dr As DataRow = dsDataSet.Tables("CustomerTable").Select("Name= '" & txtMyTextBox.Text & "'")(0)
0
 
RonaldBiemansCommented:
Sorry but dt.select doesn't return a column but a row

What are you trying to achieve exactly?
0
 
RonaldBiemansCommented:
this works though


Dim dr As DataRow = ds.Tables(0).Select("Firstname = '" textbox1.text & "'")(0)
msgbox dr.item("lastname")
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
stainesAuthor Commented:
Thankyou for your reply.

Dim dr As DataRow = ds.Tables(0).Select("Firstname = '" textbox1.text & "'")(0)

I tried, but couldn't compile it as there is an error ,it said " Comma,')', or valid expression continuation expected" ?

perhaps i should explain abit better?

I want to get the Text from the textBox i created (called "txtMyTextBox"), and look into the dataset table called "CustomerTable", in column called "Name".

Once it found and select that row, i want it to return the item on column say "Lastname" as u did " msgbox dr.item("lastname") "

At the moment i can select the item in the column ONLY if i know what the item is called.
e.g. Dim row As DataRow = dt.Select("Name =  'John' ")(0)
but instead of John, i want the string to be pass from txtMyTextBox.Text

How to do that?

Thanks in advance.
0
 
RonaldBiemansCommented:
Hi Staines,

could you post your code

because I tested this and it worked. so maybe there is a small typo in your code

0
 
stainesAuthor Commented:
Dim dr As DataRow = dsDataSet.Tables("CustomerTable").Select("Name= '" txtMyTextBox.Text & "'")(0)

There is a blue underline on "txtMyTextBox", and when u point ur mouse to the blue underline it saids "Comma,')', or valid expression continuation expected"

the rest of code is fine.
0
 
stainesAuthor Commented:
and also how to avoid program being crash if Select couldn't find the string from txtMyTextBox.Text ?
0
 
stainesAuthor Commented:
YEP, I see. ! Thank you so much!!!!!
0
 
RonaldBiemansCommented:
just check

if isnothing(dr) then
  msgbox("not found")
else
  msgbox("found")
end if
0
All Courses

From novice to tech pro — start learning today.