Ive been trying to get this working for about 6 hours now and cant seem to find the solution or the error in my code...
Basically the code SHOULD pick up every instance of a date/time that is 2 minutes past the current date/time but for some reason it only returns one instance. Ive tried so many ways of solving this i couldnt possibly write them all down.
I have two methods removedexpiredusers() and getexpiredusersdate().
Getexpiredusersdate() should retrieve each records date/time added and feed them through to removeexpiredusers() which should use that method to filter out which records are two minutes past the current date and if they are delete them.
The two minutes deletion time is purely for testing as the plan is to change this to delete the user account after 24 hours giving the user a temporary account for one day.
Here is my code so far any help appreciated
private void removeExpiredUsers()
{
while (DateTime.Now >= getExpiredUsersDate().AddMinutes(2))
{
//remove the user
dal.spDeleteExpiredUsers(getExpiredUsersDate());
}
}
private DateTime getExpiredUsersDate()
{
DateTime date = new DateTime();
try
{
using (SqlDataReader datareader = dal.getUsers())
{
while (datareader.Read())
{
date = ((DateTime)datareader["DateAdded"]);
MessageBox.Show(date.ToString());
}
}
}
catch (SqlException sqlex) { }
catch (Exception ex) { }
return date;
}
EDIT
Im going to include the stored procedures here also guys
GetUsers
Create proc spGetUsers
as
Begin
SELECT *
FROM [User]
ORDER BY Username
End
*spDeleteExpiredUsers
Create proc spDeleteExpiredUsers
@DateAdded datetime
as
Begin
DELETE
FROM [User]
WHERE DateAdded = @DateAdded
End
try-catchblocks so that we can debug better.