Tsjallie

Tsjallie

  • NA
  • 21
  • 1.3k

OleDbDataReader throws error: Missing Parameters

Nov 10 2019 3:28 PM
Hi all!
New on his forum. I'm learning C# for about a month now. Met a lot of problems of course, but so far managed to crack 'm all by trying and searching the internet (that's just the way I learn best and how I mastered vba before).
But (hate to admit) now I've stumbled upon an issue I can't seem te solve nor have I been able to find a solution on the internet.
So need some help here.

I have a method retrieving 0 or more records from an Access db which works fine. Retrieved data is moved to datagridview cells.
  1. private void Calendar_PopulatePeriod(DateTime Period)  
  2. {  
  3.     OleDbCommand cmd = new OleDbCommand("SELECT [PropertyId], [Period], [CalendarText], [StayId] " +  
  4.                                         "FROM EVBookings " +  
  5.                                         "WHERE [Period] = @PeriodCol " +  
  6.                                         "ORDER BY [Period]", conn);  
  7.     cmd.Parameters.AddWithValue("@PeriodCol", Period);  
  8.     OleDbDataReader rdr = cmd.ExecuteReader();  
  9.     int b = 0;  
  10.     while (rdr.Read())  
  11.     {  
  12. ome code....  
  13.     }  
  14. }  
 
Then I have a second method retrieving 1 record which I based on this working method, but which just won't work.
Retrieved data should go to text boxes on a form.
I keep getting this error message: 
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Values for one or more required parameters are missing.

This is the code of the method which is syntactically equal to the first mentioned (working) method:
  1. private void GetStayDetails(double StayId)  
  2. {  
  3.     OleDbCommand cmd = new OleDbCommand("SELECT [GuestName] , [Date In], [Date Out], [OpenEnded], " +  
  4.                                                `[First Month Rent], [Security Deposit], [Monthly Rent], " +  
  5.                                                 "[First Month Rent To Owner], [Monthly Rent To Owner], " +  
  6.                                                 "[ES Mgt Fee] " +  
  7.                                         "FROM Stays " +  
  8.                                         "WHERE StayId = @StayId", conn);  
  9.     cmd.Parameters.AddWithValue("@StayId", StayId);  
  10.     OleDbDataReader rdr = cmd.ExecuteReader();  
  11.     while (rdr.Read())  
  12.     {  
  13.         Debug.WriteLine("==> " + rdr.GetValue(0));  
  14.     }  
  15. }  
When searching for what's causing the error I tried the following alternative sql commands:
  1. OleDbCommand cmd = new OleDbCommand("SELECT [StayId], [GuestName] FROM Stays", conn);  
  2. OleDbCommand cmd = new OleDbCommand("SELECT [StayId], [GuestName] FROM Stays", conn);OleDbCommand cmd = new OleDbCommand("SELECT * FROM Stays WHERE [StayId] = @StayId", conn);  
  3. OleDbCommand cmd = new OleDbCommand("SELECT [StayId], [GuestName] FROM Stays WHERE [StayId] = 6500010", conn);  
All three work fine.
But this one gives the error again:
  1. OleDbCommand cmd = new OleDbCommand("SELECT [StayId], [GuestName] FROM Stays WHERE [StayId] = @Stayid", conn);  
I have not a clue why the sql in one method works fine but not in the other.
Let me know if I need to supply more detail.
 
Cheers!
Tsjallie

Answers (7)