Solved

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

Posted on 2007-04-03
2
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
tusharashah earned 500 total points
ID: 18844884
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
ID: 18845358
Thanks a ton tusharashah... it works great!

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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