Get the value from textbox to SQL query

I have the following code that works with adsolute text butwhen i try and take a value from the textbox it doesnt get the value.

 Dim Search As String = txtSearch.Text
 Me.OleDbSelectCommand2.CommandText = "SELECT title, subject, author, year FROM title WHERE title.subject = '" & Search & "'"
       
grind33Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RonaldBiemansConnect With a Mentor Commented:
where is this declared

Dim Search As String = txtSearch.Text
 Me.OleDbSelectCommand2.CommandText = "SELECT title, subject, author, year FROM title WHERE title.subject = '" & Search & "'"

Is this in the windows generated code

because at that moment txtsearch.text doesn't have a value yet
0
 
Bob LearnedCommented:
Are you getting an error?

Bob
0
 
grind33Author Commented:
nope just not displaying anything in the datagrid :S
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
Bob LearnedCommented:
Is this coming from Northwinds sample database?  SQL Server or Access?  ASP.NET or WinForms?

Bob
0
 
Carl TawnSystems and Integration DeveloperCommented:
What type of database are you connecting to ?  Have your tried outputting the SQL query to the debug window to make sure it is being populated properly ?
Did you mean to do an exact search ? The query yu have will only return results that EXACTLY match the value in "Search".

Maybe something like:

    SELECT title, subject, author, year FROM title WHERE title.subject LIKE '%" + Search + "%'"

might be more appropriate.


Hope this helps.
0
 
Bob LearnedCommented:
Good morning, Carl :)

Bob
0
 
grind33Author Commented:
Sorry, it does come up with the error:

The variable 'Search' was either undeclared or never assigned!

its from an access database.

Carl that works but not properly because it just sets an empty value so it displays all.
0
 
RonaldBiemansCommented:
Even worse the code will be removed entirly be the IDE. That is probably why it gives the error, The variable 'Search' was either undeclared or never assigned!
0
 
grind33Author Commented:
Yes, so how do you go about getting the textbox value into the SQL
0
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Move your code somewhere else, like the Form Load event for example.
0
 
RobertRFreemanCommented:
Some tips on database access.

http://vsnetdatabinding.blogspot.com/
0
 
Bob LearnedCommented:
Good blog, Robert :)

Bob
0
 
OutsideTheBoxCommented:
it looks like you may need another quotation mark

'" & Search & "'"

should be:

"'" & Search & "'"
0
All Courses

From novice to tech pro — start learning today.