Gizmo

Gizmo

  • NA
  • 4
  • 9.3k

oledbCommand parameters causing frustration - simple problem

May 5 2006 1:54 PM
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!!