c# - schedule for update date in asp.net -


i want make schedule run after every 24 hours in asp.net (global.asax). want check current date , date store database 1 one. if date less current date add 7 days it.

i trying without close reader update not possible. kind of error shows me every time.

private static void task() {     string cs = configurationmanager.connectionstrings["dbcs"].connectionstring;     using (sqlconnection con = new sqlconnection(cs))     {         con.open();         datetime newdate;         string ct = datetime.now.tostring("dd/mm/yyyy");         datetime ct = datetime.parseexact(ct, "dd/mm/yyyy", cultureinfo.invariantculture);         sqlcommand cmd1 = new sqlcommand("select date tblinsertdate", con);         cmd1.executenonquery();         sqldatareader rdr = cmd1.executereader();         while (rdr.read())         {             datetime dt = datetime.parseexact(rdr["date"].tostring(), "dd/mm/yyyy", cultureinfo.invariantculture);             int result = datetime.compare(dt, ct);             if (result < 0)             {                 newdate = dt.adddays(7);                 sqlcommand cmd2 = new sqlcommand("update tblinsertdate set date=@newdate", con);                 cmd2.parameters.addwithvalue("@newdate", newdate);                 cmd2.executenonquery();             }         }     } } 

i make schedule in global.asax of timer.

you have written update query using same connection object inside reader.read(). means reader have open connected connection while reading data , @ same time command trying update data using same connection. either can fixed creating connection object in same loop or same connection object after reader close. have made small change, try use this:- , placed update query out of reader loop

private static void task() {     string cs = configurationmanager.connectionstrings["dbcs"].connectionstring;     using (sqlconnection con = new sqlconnection(cs))     {         con.open();         datetime newdate;         string ct = datetime.now.tostring("dd/mm/yyyy");         datetime ct = datetime.parseexact(ct, "dd/mm/yyyy", cultureinfo.invariantculture);         sqlcommand cmd1 = new sqlcommand("select date tblinsertdate", con);         cmd1.executenonquery();         sqldatareader rdr = cmd1.executereader();        list<datetime> dates=new list<datetime();         while (rdr.read())         {             datetime dt = datetime.parseexact(rdr["date"].tostring(), "dd/mm/yyyy", cultureinfo.invariantculture);             int result = datetime.compare(dt, ct);             if (result < 0)             {                 newdate = dt.adddays(7);                 dates.add(newdate);              }         }         foreach(datetime dt in dates)         {                 sqlcommand cmd2 = new sqlcommand("update tblinsertdate set date=@newdate", con);                 cmd2.parameters.addwithvalue("@newdate", dt);                 cmd2.executenonquery();         }     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -