Josh
Josh

Reputation: 115

Using multiple Parameters after WHERE in SELECT query

I'm trying to avoid populating a table and using multiple queries and statements to select a specific ID representing a Holiday from a DataGridView.

Is what I'm trying to do even possible?

The code I've got so far is as follows :

string HolidayIDQuery = "SELECT ID FROM [Holiday] WHERE PayrollNo=@PayrollNo + StartDate=@StartDate + EndDate=@EndDate"; 
OleDbCommand GetHolID = new OleDbCommand(HolidayIDQuery, conn);
GetHolID.Parameters.AddWithValue("@PayrollNo", GotPayroll);
GetHolID.Parameters.AddWithValue("@StartDate", OleDbType.Date).Value = Convert.ToDateTime(dataGridView1.Rows[LineIndex].Cells[1].Value);
GetHolID.Parameters.AddWithValue("@EndDate", OleDbType.Date).Value = Convert.ToDateTime(dataGridView1.Rows[LineIndex].Cells[2].Value);
int HolID = Convert.ToInt32(GetHolID.ExecuteScalar());

I get this Error on the bottom line.

Exception thrown: 'System.Data.OleDb.OleDbException' in System.Data.dll

Additional information: Data type mismatch in criteria expression.

Upvotes: 0

Views: 2648

Answers (2)

Bigjim
Bigjim

Reputation: 2255

Seems to be an error in your query. Instead of

SELECT ID 
FROM [Holiday] 
WHERE PayrollNo=@PayrollNo + StartDate=@StartDate + EndDate=@EndDate

try this:

SELECT ID FROM [Holiday] 
WHERE PayrollNo=@PayrollNo 
      AND StartDate=@StartDate 
      AND EndDate=@EndDate"

Upvotes: 1

mikeb
mikeb

Reputation: 11267

You can use AND in your WHERE clause:

WHERE PayrollNo=@PayrollNo AND StartDate=@StartDate AND EndDate=@EndDate;

You might also want to try an SQL tutorial: https://www.google.com/webhp?q=sql%20tutorial

Upvotes: 4

Related Questions