0

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.

2
  • 1
    Use and instead of +! Commented Mar 15, 2016 at 11:26
  • 1
    + is not the same as AND Commented Mar 15, 2016 at 11:27

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

i'm not sure why this was downvoted - can the downvoter explain?
Cheers everyone! Can't believe I didn't try this! Thanks! EDIT: I'll be marking this as an answer as soon as I am able
1

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"

1 Comment

Thank you, I've done this and it works great! Can't believe I hadn't tried this yet!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.