WP7 - How to add rows to a table existing in a sterling database?

Jan 3, 2011 at 11:29 PM

Can someone please post some sample code to add new rows to an existing table in sterling?

Jan 3, 2011 at 11:38 PM
Edited Jan 4, 2011 at 5:04 PM
    public void UpdateAttendance(object param)
    {
        AttendanceForDay objAttendanceForDay = (AttendanceForDay)param;

        // Get the Attendnace record if there is one
        Attendance objAttendanceRecord = 
            SterlingService.Current.Database.Load<Attendance>(objAttendanceForDay.AttendanceKey);

        if (objAttendanceRecord == null) // New Record
        {
            // If Attendance Record was not found create a new record
            Attendance NewAttendanceRecord = new Attendance();
            NewAttendanceRecord.AttendanceKey = objAttendanceForDay.AttendanceKey;
            NewAttendanceRecord.AttendanceDate = AttendanceDate;
            NewAttendanceRecord.EnrollmentId = objAttendanceForDay.EnrollmentId;
            NewAttendanceRecord.AttendanceStatus = objAttendanceForDay.AttendanceStatus;
            SterlingService.Current.Database.Save(NewAttendanceRecord);
            SterlingService.Current.Database.Flush();
        }
        else // Update Record
        {
            // Set Attendanace status and save the record
            objAttendanceRecord.AttendanceStatus = objAttendanceForDay.AttendanceStatus;
            SterlingService.Current.Database.Save(objAttendanceRecord);
            SterlingService.Current.Database.Flush();
        }
    }

Jan 4, 2011 at 2:59 AM

Thanks.

I am able to add new rows in the DB when the App is activated (in WP7 Emulator) for the 1st time. From the second time onwards, when I add a new row it overwrites the 1st row in addition to adding a new row to the table (Type). Am I doing everying right in the App.xaml.cs?  do I need to check for the presence of the DB before calling RegisterDatabase<ClassType>?

 _engine = new SterlingEngine();
 _logger = new SterlingDefaultLogger(SterlingLogLevel.Information);
 _engine.Activate();
 _database = _engine.SterlingDatabase.RegisterDatabase<TClassype>();
Coordinator
Jan 4, 2011 at 3:27 PM

Please take a look here and make sure you are following the setup in the App.xaml.cs to handle tombstoning:

http://csharperimage.jeremylikness.com/2010/12/using-sterling-in-windows-phone-7.html