Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataTable select method, how to select with a variable?

Posted on 2004-09-24
9
Medium Priority
?
412 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:staines
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12140889
Sorry but dt.select doesn't return a column but a row

What are you trying to achieve exactly?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12141042
this works though


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

Author Comment

by:staines
ID: 12144055
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12144342
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
 

Author Comment

by:staines
ID: 12144762
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
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 200 total points
ID: 12144833
oeps, this is correct

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

Author Comment

by:staines
ID: 12144837
and also how to avoid program being crash if Select couldn't find the string from txtMyTextBox.Text ?
0
 

Author Comment

by:staines
ID: 12144858
YEP, I see. ! Thank you so much!!!!!
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12144954
just check

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question