Quote Originally Posted by jre1229
Hi,

I have this



VB Code:
  1. Dim FindRecordDA As New OleDbDataAdapter("SELECT * FROM tblSSOPVerificationFormRecords WHERE AutoNumber = ? AND Technician = ? AND Location = ? AND VerificationDate >= ? AND VerificationDate <= ?", cn)
  2.  
  3.         Dim FindRecordDS As New DataSet
  4.  
  5.  
  6.  
  7.         FindRecordDS.Clear()
  8.  
  9.         FindRecordDA.SelectCommand.Parameters.Clear()
  10.  
  11.  
  12.  
  13.         If recordNumberTextBox.Text = "" Then
  14.  
  15.             FindRecordDA.SelectCommand.Parameters.Add("@AutoNumber", OleDbType.VarChar, 80).Value = "*"
  16.  
  17.         Else
  18.  
  19.             FindRecordDA.SelectCommand.Parameters.Add("@AutoNumber", OleDbType.VarChar, 80).Value = recordNumberTextBox.Text
  20.  
  21.         End If
  22.  
  23.  
  24.  
  25.         FindRecordDA.SelectCommand.Parameters.Add("@Technician", OleDbType.VarChar, 80).Value = technicianComboBox.Text
  26.  
  27.         FindRecordDA.SelectCommand.Parameters.Add("@Location", OleDbType.VarChar, 80).Value = locationComboBox.Text
  28.  
  29.         FindRecordDA.SelectCommand.Parameters.Add("@VerificationDate1", OleDbType.Date, 80).Value = SSOPStartDatePicker.Text
  30.  
  31.         FindRecordDA.SelectCommand.Parameters.Add("@VerificationDate2", OleDbType.Date, 80).Value = SSOPEndDatePicker.Text
  32.  
  33.  
  34.  
  35.         FindRecordDA.FillSchema(FindRecordDS, SchemaType.Source, "tblSSOPVerificationFormRecords")
  36.  
  37.         FindRecordDA.Fill(FindRecordDS, "tblSSOPVerificationFormRecords")



I know the "*" isn't correct but how can I make it to where that parameter is nothing and pulls everything?
Definately change your code back to the original code above...but you will need to modify it. Try always using named parameters even if the RDBMS doesn't support em, at least on your client end you know what and where everything is. If you need to include an all, you will need to make use of the DBNull class and pass DBNull.Value. Make sure you do not pass "*" like you did, and definately avoid Nothing keyword, this keyword is unpredictable when dealing with databases and nulls.

So now you should have:

Code:
        If recordNumberTextBox.Text.Length = 0 Then

            FindRecordDA.SelectCommand.Parameters.Add("@AutoNumber", OleDbType.VarChar, 80).Value = DBNull.Value

        Else

            FindRecordDA.SelectCommand.Parameters.Add("@AutoNumber", OleDbType.VarChar, 80).Value = recordNumberTextBox.Text

        End If
So now you're done with your code page...you will need to do one more thing in your db side of things to make sure this works...
In your db you need to write your procedure so that the parameter has a default value of null and use some boolean logic...

In the db side of things:

CREATE PROCEDURE foo_bar @AutoNumber int=NULL
AS
BEGIN
SET NOCOUNT ON
SELECT blah FROM YourTable WHERE
(@AutoNumber IS NULL OR YourTable.AutoNumber = @AutoNumber)
...

That's all