Solved

How do I reference the content of a textbox as the search criteria in an SQL statement?

Posted on 2007-04-03
2
177 Views
Last Modified: 2010-03-19
I have been playing around with the 'pubs' sample SQL database (to help me learn SQL). I have a dropdownlist called 'oAuthors' and a textbox called 'TextBox1'.

I want to be able to put the last name of an author from the 'Authors' table into TextBox1, click a button, and have this author's name data pull from the SQL table and bind to the 'oAuthors' dropdownlist----thus, it would be the  only name in the dropdownlist. The way that I am referring to TextBox1.Text in the code gives me an error that says that TextBox1.Text is not a column name in the table.

How can I reference the actual 'content' of this textbox? Thanks....

Here is my pageload code:


<script language="VB" runat="server">

Sub Page_Load(Source As Object, E As EventArgs)
  if Not Page.IsPostBack Then
    Dim oConn As SQLConnection
    Dim oComm As SQLCommand
    Dim oReader As SQLDataReader
    Dim sSQL As String
 
    sSQL= "SELECT au_id, au_fname + ' ' + au_lname AS AuthorName "
    sSQL+= "FROM Authors WHERE au_lname = TextBox1.Text"
    sConn= "server=(local);database=pubs;Trusted_Connection=yes"
   

    oConn = New SQLConnection(sConn)
    oConn.Open()

    oComm = New SQLCommand(sSQL,oConn)
    oReader = oComm.ExecuteReader()

    oAuthors.DataSource = oReader
    oAuthors.DataBind()
   
  end if
End Sub

</script>



For example, it works if I do it the following way:

"FROM Authors WHERE exists au_lname = 'Flintstone'" ..... then it will work.

However, I want the flexibility of being able to specify using only the last name....thanks....

PS:  Also, a msgbox that said "name not found" or something like that would be a nice too if the query had no matching name found....

0
Comment
Question by:jazjef
2 Comments
 
LVL 18

Accepted Solution

by:
tusharashah earned 500 total points
Comment Utility
1) You can concatnate your Textbox value in your sSQL string like following:
    sSQL= "SELECT au_id, au_fname + ' ' + au_lname AS AuthorName "
    sSQL+= "FROM Authors WHERE au_lname = '" + TextBox1.Text + "'"

2) You can check HasRows property of DataReader for this
    oComm = New SQLCommand(sSQL,oConn)
    oReader = oComm.ExecuteReader()
     If dr.HasRows Then
       oAuthors.DataSource = oReader
       oAuthors.DataBind()
     Else
       Label1.Text = "Name not found"
     End If
0
 
LVL 4

Author Comment

by:jazjef
Comment Utility
Thanks a ton tusharashah... it works great!

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now