views:

299

answers:

0

I have a dynamic sql query with the following WHERE clause:

...
"WHERE " & _
  "mc.EmployerCode = @EmployerCode " & _
  "AND mc.ReceiptDate = #02/SEP/2009# " & _
  "AND " & _
  "( " & _
    "f.Payment = 'COMPLETE' " & _
    "OR f.Payment IS NULL " & _
  ") " & _
...

As you can see, there are a few joins in this query.

Now, if I use:

Dim cmd As New OleDb.OleDbCommand()

cmd.Parameters.AddWithValue("@EmployerCode", employerCode)

cmd.CommandText = query
cmd.CommandType = CommandType.Text

Dim db As Database = DatabaseFactory.CreateDatabase()
Return db.ExecuteDataSet(cmd)

I get a "No value given for one or more required parameters." exception.

If I use:

Dim cmd As New OleDb.OleDbCommand()

cmd.CommandText = query
cmd.CommandType = CommandType.Text

Dim db As Database = DatabaseFactory.CreateDatabase()
db.AddInParameter(cmd, "@EmployerCode", DbType.String, employerCode)
Return db.ExecuteDataSet(cmd)

I get a "No value given for one or more required parameters." exception.

If I use:

Dim cmd As New OleDb.OleDbCommand()

cmd.Parameters.AddWithValue("@EmployerCode", employerCode)

cmd.CommandText = query
cmd.CommandType = CommandType.Text

Dim db As Database = DatabaseFactory.CreateDatabase()
db.AddInParameter(cmd, "@EmployerCode", DbType.String, employerCode)
Return db.ExecuteDataSet(cmd)

The code runs fine and doesn't complain!

My question is: am I supposed to use both AddWithValue and AddInParameter?