Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Lets say I have a table called adverts with the following fields:advert_id, name,date_created description,duration,status,user_id, cat_id.

Let's say a user puts a duration of 1 week in the duration field and its status is set to active. After a week has passed, the status is set to inactive in the table.

Can anyone tell me if it's possible to do this with asp.net 4.0 code using visual basic in visual studio 2010 with web forms?
Posted
Comments
Peter Leow 24-Oct-14 23:50pm    
First tag your question properly.
I assume you are using some database to store the table. THere is no need for countdown. Whenever a user try to access a particular adverts record in the table, always check the current date against the "date_created" and the "duration" fields. If it has exceeded, then update the "status" field to "inactive".
Minhaaj Edoo 25-Oct-14 0:10am    
Very sorry for that, i forgot to tag sql server.
Minhaaj Edoo 25-Oct-14 0:12am    
If you know of any sample code it would be of great help
Sergey Alexandrovich Kryukov 25-Oct-14 0:28am    
Not clear what the idea of countdown can do here...
—SA

You don't necessarily need to have a column in your database table for status, you could use an aggregated column when you do queries from the table.
In this way there is no need update the table.

advert_idnamedate_createddescriptiondurationuser_idcat_id
1Whatever2014-10-15The status will be inactiveP7D58
1Whatever2014-10-25The status will be activeP7D79


Note For duration I am using xsd:duration[^] format and the column type should be VARCHAR(25).

In your query you can aggregate the status using a calculation.
As I mostly work in MySQL I use this syntax, but it should be possible to translate the code to other SQL flavors.
SQL
SELECT `name`, IF(DATE_SUB(CURDATE(), INTERVAL TRIM(TRAILING 'D' FROM SUBSTRING(duration, 2)) DAY) > date_created, 'Inactive', 'Active') AS `status` FROM adverts;

(The way to get the number of days out of duration can be improved.)

You can also do this in c#.
C#
DataTable dt = new DataTable();
dt.RowChanged += dt_RowChanged; // Add an event that fires for every new row

dt.Columns.Add("advert_id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("date_created", typeof(DateTime));
dt.Columns.Add("duration", typeof(TimeSpan));
dt.Columns.Add("status", typeof(string));

// Here you would use something like
//DbDataAdapter da = new SqlDataAdapter();
//da.SelectCommand = new SqlCommand("SELECT advert_id, name, date_created, duration, '' AS status FROM adverts");
//da.Fill(dt);

// Instead I add the rows manually
dt.Rows.Add(1, "Row1", DateTime.Now.Subtract(new TimeSpan(10, 0, 0, 0, 0)), new TimeSpan(7, 0, 0, 0, 0));
dt.Rows.Add(2, "Row2", DateTime.Now, new TimeSpan(7, 0, 0, 0, 0));


The event handler
C#
static void dt_RowChanged(object sender, DataRowChangeEventArgs e)
{
    if (e.Action == DataRowAction.Add)
    {
        DateTime created = (DateTime)e.Row["date_created"];
        TimeSpan duration = (TimeSpan)e.Row["duration"];
        e.Row["status"] = (created > DateTime.Now.Subtract(duration)) ? "Active" : "Inactive";
    }
}
 
Share this answer
 
v2
Perfect answer. Thank you very much!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900