Solved

Sql Statement for searches

Posted on 2006-11-02
15
187 Views
Last Modified: 2010-04-23
Hello, I have the following SQL Statement, I will have about 8 different textboxes, for different search criteria, firstname, lastlame, amountpaid, etc...  I will just use 2 for the example though.  Right now, if I put in the first and lastname, it finds them.  If I only put one or the other in, it throws an error, saying that one of them was null.  I would like the user to be able to search by any of the criteria, making none of them required.

DsSearch1.Clear()
            Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo WHERE firstname='" & txtfirstname.Text & "' Or lastname='" & txtlastname.Text & "';"
            Cmd.CommandType = CommandType.Text
            Da = New OleDb.OleDbDataAdapter(Cmd)
            DGResults.DataSource = DsSearch1.Receiptinfo
            Da.Fill(DsSearch1.Receiptinfo)
0
Comment
Question by:seanrhudy
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17857554
Why dont you create the SQL WHERE clause as

Dim strWhere as string

If txtfirstname.Text <> "" Then
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 17857558
           Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo WHERE"
dim inserted as boolean =false
if firstname.textlength>0 then
Cmd.CommandText &= firstname='" & txtfirstname.Text & "'"
inserted=true
end if

if lastname.textlength>0 then
Cmd.CommandText &= iif(inserted," OR ","") & lastname='" & txtfirstname.Text & "'"
inserted=true
end if
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 250 total points
ID: 17857585
Sorry bit quick on the enter key ...

something like this ..

Dim strSQL as string = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo "

Dim strWHERE as string = ""

If txtfirstname.Text <> "" Then
  if strWHERE <> "" then strWHERE &= " OR "
  strWHERE &= " firstname='" & txtfirstname.Text & "'"
End if

If txtlastname.Text <> "" Then
  if strWHERE <> "" then strWHERE &= " OR "
  strWHERE &= " lastname='" & txtlastname.Text & "'"
End if

If strWHERE <> "" Then
  strSQL = " WHERE " & strWHERE
End If
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:seanrhudy
ID: 17858200
What does the last statement do?
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17858248
sorry, should be :

 strSQL &= " WHERE " & strWHERE
0
 

Author Comment

by:seanrhudy
ID: 17858420
What does that if statement do though?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858451
the code below checks if there is any condition in the WHERE clause and if there is, it adds the WHERE clause to the sql statement:

If strWHERE <> "" Then
  strSQL = " WHERE " & strWHERE
End If
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858461
for example if your where clause is "lastname='Smith',

and your sql statement is "SELECT * FROM TAble1"

then  that last statement will convert your sql statement to

"SELECT * FROM Table1 WHERE lastname='Smith'
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17858511
It makes sure that only the desired columns are searched on and not all by default.
So as YZlat states you create the main SELECT SQL statement first then add on the customised WHERE clause after to create the full SQL query.
0
 

Author Comment

by:seanrhudy
ID: 17858637
ok, thank you, I dont like to just copy and paste code, I like to understand it first, makes it easier later....
0
 

Author Comment

by:seanrhudy
ID: 17863370
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim Da As OleDb.OleDbDataAdapter
        Try
            Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/backup/receiptprogram/receiptmanagement/receipt.mdb"
            Con.Open()
            Cmd.Connection = Con
        Catch ex As Exception
        End Try

        Try

            DsSearch1.Clear()
            Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo "
            Cmd.CommandType = CommandType.Text
            Da = New OleDb.OleDbDataAdapter(Cmd)
            DGResults.DataSource = DsSearch1.Receiptinfo
            Da.Fill(DsSearch1.Receiptinfo)

            Dim strSQL As String = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo "

            Dim strWHERE As String = ""

            If txtfirstname.Text <> "" Then
                If strWHERE <> "" Then strWHERE &= " OR "
                strWHERE &= " firstname='" & txtfirstname.Text & "'"
            End If

            If txtlastname.Text <> "" Then
                If strWHERE <> "" Then strWHERE &= " OR "
                strWHERE &= " lastname='" & txtlastname.Text & "'"
            End If

            If strWHERE <> "" Then
                strSQL &= " WHERE " & strWHERE
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Con.Close()
            Cmd.Connection = Nothing
        End Try
    End Sub

It is bringing back a null value for the total column, when I know there is a value in there
0
 

Author Comment

by:seanrhudy
ID: 17863490
Ok, I fixed that problem, but no matter what I search for, everything row in the db is found.
0
 

Author Comment

by:seanrhudy
ID: 17863950
*correction, Every row in the database is found, and displayed.
0
 

Author Comment

by:seanrhudy
ID: 17865755
Ok its working, I changed the commandtext to "cmd.commandtext = strSQL and I put all the dataset code after the strSQL and where statements.  Thanks!!
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17865771
no problem.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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