Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

oledbCommand parameters causing frustration

Hello.

I am using an Access database. Recently I learnt that it is not a good idea to pass information directly into an SQL query... and it is a good practice to use parameters instead.

Note1: rentChecked_cmd is an OleDBCommand object
Note2: I've already created the Parameter in InitialiseComponents() using the following statement:
rentChecked_cmd.Parameters.Add(new OleDbParameter("RentID", Integer));

Well, heres my code, I've kept it simple

private void returnBtn_Click(object sender, System.EventArgs e)
{
rentChecked_cmd.Parameters["RentID"].Value=int.Parse(whatever);

rentChecked_cmd.CommandText = "SELECT DateDue FROM Rents WHERE RentID=@RentID";

mainConn.Open();

reader = rentChecked_cmd.ExecuteReader();
...
...
...

}

When I click the "Return" button for the first time, everything works fine... but when I click it again.. the database tells me "Accessor is Invalid". After debuggined the error is throw by the
reader = rentChecked_cmd.ExecuteReader() line

This error is frustrating me cause I cant find any reason! Whats funny is that in my returnBtn_Click() method when I add the line "rentChecked_cmd.Parameters.Add(new OleDbParameter("RentID", Integer));"... it works... But thats so stupid, I am not supposed to Add a new Parameter each time a button is clicked!!
[1414 byte] By [Gizmo] at [2007-11-11 8:37:44]
# 1 Re: oledbCommand parameters causing frustration
I would remove the Parameters.Add from InitializeComponents and add the parameter in the button click event, just before executing the query.
Phil Weber at 2007-11-11 21:47:15 >
# 2 Re: oledbCommand parameters causing frustration
I would remove the Parameters.Add from InitializeComponents and add the parameter in the button click event, just before executing the query.

uhh...
Maybe I am missing the point, but isnt it EXTREMELY stupid to add a new Parameter EACH time a button is clicked?
Why should I add the SAME Parameter with each button click, doesnt it make more sense to just change an existing Parameter's value in the button click event?
Gizmo at 2007-11-11 21:48:10 >
# 3 Re: oledbCommand parameters causing frustration
No, I don't think it's "EXTREMELY stupid" to do so; I can think of many things that are much worse. ;-) Have you benchmarked both approaches? How much time do you save by creating the parameter only once?

The primary advantage of creating the parameter on each query execution is that it works. ;-)
Phil Weber at 2007-11-11 21:49:09 >
# 4 Re: oledbCommand parameters causing frustration
No, I don't think it's "EXTREMELY stupid" to do so; I can think of many things that are much worse. ;-) Have you benchmarked both approaches? How much time do you save by creating the parameter only once?

The primary advantage of creating the parameter on each query execution is that it works. ;-)

Cool. I did it that way and everything is working fine.

Its just that lately I've been trying to "clean up" my style of coding by doing things the "professional" way. Before I parameterized the SQL queries, my program was working fine... but I just wanted to do it the "right way".

Thanks again.
Gizmo at 2007-11-11 21:50:17 >