In my WCF web service I have been reading from the database with no trouble at all. Today I attempted an update but quickly discovered the changes weren’t actually being saved to the database. I am getting no errors or exceptions thrown. I have spent the last several hours researching the problem with no luck. I have found many similar questions but most of he time the reason was that the table being written to didn’t have a primary key, however this is not the case with this table.
Here’s some code:
var db = new dbDataContext(Convert.ToString(ConfigurationManager.AppSettings["sConn"]));
...
var bookingRow = (from mBooking in db.d_bookings
where mBooking.booking_id == bookingID
select new BookingResult
{
guideID = mBooking.user_id_guide == null ? 0 : (int)mBooking.user_id_guide,
adultShow = mBooking.booking_nAdults_show == null ? 0 : (int)mBooking.booking_nAdults_show,
childShow = mBooking.booking_nChild_show == null ? 0 : (int)mBooking.booking_nChild_show,
}
).Single();
bookingRow.guideID = someNewValue;
bookingRow.adultShow = someNewValue;
bookingRow.childShow = someNewValue;
try
{
db.SubmitChanges();
}
catch (Exception ex)
{
return "{\"result\" : \"false\" }";
}
And the BookingResult class is simply:
public class BookingResult
{
public int guideID { get; set; }
public int adultShow { get; set; }
public int childShow { get; set; }
}
This is my first project working with WCF and LINQ and it seemed incredibly straight forward for the most part but this has thrown me for a loop. Any help is greatly appreciated!
SOLVED: The problem was that I was reading into the custom class which wasn’t attached to my DataContext. Removing the class and just selecting the entire row works fine.
var bookingRow = (from mBooking in db.d_bookings
where mBooking.booking_id == bookingID
select mBooking
).Single();
You’re creating a completely new
BookingResultthat would not be attached to theDataContextand therefore will not be considered as a change when you callSubmitChanges. Is there a table in the database calledBookingResults? What do you hope will happen to your newBookingResult? Would it create a new row?