I am new to LINQ and am liking it so far, however I am trying to load in some forms by requesting a single record (student). If this doesn't exist I want't default values i.e. empty strings false bools...
so I used:
db = new DataClassesDataContext();
student = db.ss_students.SingleOrDefault(p => p.student_id == studentID);
txtRegNumber.EditValue = student.car_reg_no;
This failed on the assignment of student.car_reg_no. I realised I seem to have mis-understood the SingleOrDefault method and it actually returns null for the student if it can't find a record. I for some reason was thinking it would return the default values for each of the fields such as student.car_reg_no. I think I am still thinkign in database mode.
This is not a problem I can do code like this:
db = new DataClassesDataContext();
student = db.ss_students.SingleOrDefault(p => p.student_id == studentID);
if (student != null)
{
txtRegNumber.Text = student.car_reg_no;
//assign more control values
}
And the defaults could either be assigned to the controls in an else or assigned direct on the form.
But is this the right way to do it or am I missing something?
EDIT Thanks for the posts so far I have started the approach suggested by Marc Gravell. I have now got a bit further.
I am now trying to save the data back to the database how would I know if I am updating or inserting a record. Should I be adding bools to record this myself or is their a built in way.