c# - Trying to update single row in SQL database using LINQ to SQL using a WCF service -


what i'm trying create method using wcf service go through 1 database ( transactions) grab table has todays date, add them daily sales table have 1 row per date displaying profit, daily takings, expenses, etc.

i've tried

        public void calculateprofit(string date)         {              decimal takings = 0;// not needed             decimal expenses = 0;// not needed             using (transactionclassdatacontext cont = new transactionclassdatacontext())             {                  int counter = 0;                 dailysale d = new dailysale();                 var query = (from q in cont.dailysales q.date.equals(date) select q);                 var query2 = (from r in cont.transactions r.date.equals(date) select r);                 foreach (var z in query)                 {                     counter++;                  }                 if (counter>0)                 {                          foreach (var y in query2)                         {                              takings = takings + y.price;                             expenses = expenses + 0;                              d.expenses += 0;                             d.takings += y.price;                             d.profit = d.takings - d.expenses;                             d.date = date;                              cont.dailysales.insertonsubmit(d);// update value                             cont.submitchanges();                          }                               }                 else                 {                      d.date = date;                     cont.dailysales.insertonsubmit(d);// if there isnt entry todays date, add 1                     cont.submitchanges();                 }              }          }       } } 

but throw error "cannot add entity exists."

most similar questions have said need create new instance of d in foreach, seems add loads of records m daily sales, when want 1 row updated total.

any ideas?

you should move dailysale d = new dailysale(); scope(s) used.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -