Solved

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

Posted on 2007-04-03
2
182 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 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