0

I have VB.net app that when run you select from listbox values that should then be used in select query.Then click save command button. see vb code snippet below

transport = lsttrans.SelectedItem
    leave = lstleave.SelectedItem

then query like below.Note transport and leave are dummy columns not on existing employee table.

select employeeid,'' as transport_allowance,'' as leave from employees

i wish to somehow create new query to be something close to the below using my selected values in listbox.

select employeeid,transport,leave from employees
2
  • To do that you would need to use dynamic SQL. Please read up on that. Commented May 9, 2014 at 6:29
  • Thanks let me have a look Commented May 9, 2014 at 7:25

2 Answers 2

1

Use String.Format(). Here is the example assuming transport and leave variables are already declared:

Dim myQueryTemplate As String = "SELECT employeeid, {0}, {1} FROM employees"
Dim myFinalQuery as String = String.Format(myQueryTemplate, transport, leave)

If you will debug that, the value of myFinalQuery will be:

"SELECT employeeid, transport, leave FROM employees"

Add more if needed. From my given example, the next parameter will be {2} and so on.

Sign up to request clarification or add additional context in comments.

Comments

1

Do you already know how to retrieve data from a database in VB.NET? If not then you should read up on ADO.NET.

Assuming that you know how ADO.NET works, your SQL is simply a String, so you can build it up from parts the same way you can any other String. In this case, I would suggest calling the String.Format method to insert the column names into a query template.

1 Comment

yes I do thanks.dint know how to use the string.format feature though but now I do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.