We help IT Professionals succeed at work.

Populating Where clause with TextBox value

bhlabelle used Ask the Experts™
Not sure if this is possible, but I wanted to use a FillBy query to populate a datagridview on a form I have.  The issue I have is with the Where clause.  If I hard code the item to lookup it works fine

Where TrustName = 'Smith Trust'

I run the form, press the "FillBy" button at the top, and the datagrid is filled with all the values from the Smith Trust.

However, I want to user to be able to select the TrustName from a combobox on the form.  So I figured:

Where TrustName = Combobox1.[Text] (as an aside, I didn't put the bracket around the "Text" part, vb.net does this automatically)

So now when I run the form, press the "FillBy" button at the top, I get the following message "No value given for one or more parameters"

I've tried various remedies I've found (putting single and double quotes, + signs), all to no avail.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Where did you put this?

Where TrustName = Combobox1.[Text]


Sorry, not much of an expert, but I think your answer is the FillBy appears under the TableAdapter (in the DataSet.xsd window).  In the properties window for the FillBy there is a CommandText where the Select Query goes.  I was simply giving the Where portion of the query above.


I meant I'm not much of an expert!
NorieAnalyst Assistant

You could try changing the Where clause to this.

" Where TrustName ='"& Combobox1.Text & "'"

but I'm not sure how that would work with the rest of the commandtext.


I got an error message when I closed the Query generator.  I took a screen shot and attached it.  Not sure what it means.
Analyst Assistant
It means the syntax of the query is wrong.

I think you might need to use a parameter but I'm not sure how you would do that here, or if it's possible.


That's it!  I never used parameters before.  

So I
1) changed the clause to "Where TrustName = ?"
2) added a parameter @TrustName

Then on the FillBy button event I created a string that I populated with the combobox value, and passed it as the parameter value.


Great suggestion to use a parameter.