Updatehttp://forums.asp.net/t/1784977.aspx/1?UpdateMon, 26 Mar 2012 09:39:19 -040017849774898185http://forums.asp.net/p/1784977/4898185.aspx/1?UpdateUpdate <p>This is my User Table</p> <p>FirstName</p> <p>LastName</p> <p>MoneyEarned</p> <p></p> <p>I need to set the MoneyEarned to 0 and I do not want to change the firstname and lastname that is already stored in the DB, this is my function can you anyone advise me please to rewrite the update function please.</p> <p><br> &nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void UpdateMoneyEarned(User user)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (var db = new TeachableEntities())<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user.MoneyEarned = 0;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var q = db.Users;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; db.submitchanges();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> 2012-03-25T19:05:58-04:004898224http://forums.asp.net/p/1784977/4898224.aspx/1?Re+UpdateRe: Update <p>use something like below</p> <pre class="prettyprint">public void UpdateMoneyEarned(User user) { using (var db = new TeachableEntities()) { User q = (from u in db.Users where u.UserID == user.UserID select u).FirstOrDefault(); q.MoneyEarned = 0 db.submitchanges(); } }</pre> 2012-03-25T19:48:11-04:004899068http://forums.asp.net/p/1784977/4899068.aspx/1?Re+UpdateRe: Update <p>Error&nbsp;&nbsp; &nbsp;1&nbsp;&nbsp; &nbsp;'Teachable.Models.Data.TeachableEntities' does not contain a definition for 'submitchanges' and no extension method 'submitchanges' accepting a first argument of type 'Teachable.Models.Data.TeachableEntities' could be found (are you missing a using directive or an assembly reference?)&nbsp;&nbsp; &nbsp;C:\Teachable\Solution\Teachable\teachable.web\Models\ViewModels\ResourceListingVM.cs&nbsp;&nbsp; &nbsp;534&nbsp;&nbsp; &nbsp;21&nbsp;&nbsp; &nbsp;Teachable.Web</p> <p></p> <p>I get an error here.</p> 2012-03-26T09:16:39-04:004899129http://forums.asp.net/p/1784977/4899129.aspx/1?Re+UpdateRe: Update <p>sorry you have to use SaveChanges instead. and use Single() instead of FirstorDefault</p> <pre class="prettyprint">public void UpdateMoneyEarned(User user) { using (TeachableEntities db = new TeachableEntities()) { User q = (from u in db.Users where u.UserID == user.UserID select u).Single(); q.MoneyEarned = 0 db.SaveChanges(); } }</pre> 2012-03-26T09:39:19-04:00