Search code examples
c#winformssql-server-ce

Getting expiredate from table if equal or less then today SQL Server CE


Some help would be appreciated.

I have a table tblItem with columns ExpireDate (DateTime), AlertDays (Int).

Each Item I insert has an expiredate, I want to be alert in x days before the expiredate.

I want to count all the items where today + alertdays <= ExpireDate.

I have the following code but it is causing an error:

There was an error parsing the query. [ Token line number = 1,Token line offset = 48,Token in error = select ]

Please see my code:

internal static int getAlertDateExpire()
{
    SqlCeConnection con = ConectionAndData.Con;

    if (con.State == System.Data.ConnectionState.Closed)
        con.Open();

    int number = 0;

    string sqlCommand = "select count(*) from tblItem where (Getdate()+(select AlertDays from tblItem)) As today <= ExpireDate Order By ItemName ASC";

    SqlCeCommand com = new SqlCeCommand(sqlCommand, con);

    try
    {
        number = (Int32)com.ExecuteScalar();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
    }

    return number;
}

Solution

  • I got help and want to share the answer.

    (select count(*) from tblItem where dateadd(day,AlertDays,getdate()) >= ExpireDate)