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.
- private void Calendar_PopulatePeriod(DateTime Period)
- {
- OleDbCommand cmd = new OleDbCommand("SELECT [PropertyId], [Period], [CalendarText], [StayId] " +
- "FROM EVBookings " +
- "WHERE [Period] = @PeriodCol " +
- "ORDER BY [Period]", conn);
- cmd.Parameters.AddWithValue("@PeriodCol", Period);
- OleDbDataReader rdr = cmd.ExecuteReader();
- int b = 0;
- while (rdr.Read())
- {
- ome code....
- }
- }
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:
- private void GetStayDetails(double StayId)
- {
- OleDbCommand cmd = new OleDbCommand("SELECT [GuestName] , [Date In], [Date Out], [OpenEnded], " +
- `[First Month Rent], [Security Deposit], [Monthly Rent], " +
- "[First Month Rent To Owner], [Monthly Rent To Owner], " +
- "[ES Mgt Fee] " +
- "FROM Stays " +
- "WHERE StayId = @StayId", conn);
- cmd.Parameters.AddWithValue("@StayId", StayId);
- OleDbDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- Debug.WriteLine("==> " + rdr.GetValue(0));
- }
- }
When searching for what's causing the error I tried the following alternative sql commands:
- OleDbCommand cmd = new OleDbCommand("SELECT [StayId], [GuestName] FROM Stays", conn);
- OleDbCommand cmd = new OleDbCommand("SELECT [StayId], [GuestName] FROM Stays", conn);OleDbCommand cmd = new OleDbCommand("SELECT * FROM Stays WHERE [StayId] = @StayId", conn);
- OleDbCommand cmd = new OleDbCommand("SELECT [StayId], [GuestName] FROM Stays WHERE [StayId] = 6500010", conn);
All three work fine.
But this one gives the error again:
- 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