Пример #1
0
        /// <summary>
        /// Save this worker to the database.
        /// If this is the first save then id will be given a value.
        /// </summary>
        public void Save()
        {
            if (!MarkForSave)
            {
                return;
            }

            bool isInsert = id == 0;

            try
            {
                //do the save here to the database.
                string query = "";

                if (isInsert)
                {
                    query = "INSERT INTO tbl_ServiceUser(Firstname, Surname, idStatus, PNumber, dob, postcode, add1, add2, Notes, contactno_primary, " +
                            "contactno_secondary, idGender, location_longitude, location_latitude, medicalinfo, serviceperiodstart, serviceperiodweeks, isNotesImportant)";

                    query += " VALUES(@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17)";

                    object[] prms = new object[] { firstname, surname,
                                                   Convert.ToInt32(idStatus), PNumber,
                                                   dob_Clean,
                                                   postcode, add1, add2,
                                                   notes,
                                                   contactno_primary, contactno_secondary,
                                                   Convert.ToInt32(idGender),
                                                   LongLatCoords.Longatude, LongLatCoords.Latatude,
                                                   medicalinfo,
                                                   ServiceBegins_Clean, PeriodWeekCount,
                                                   isNotesImportant };

                    DataTable dt = Common.Database.ExecuteDatabaseQuery(query, prms, true);

                    if (dt.Rows.Count == 0)
                    {
                        throw new Exception("Failed To Insert Service User");
                    }

                    id = Convert.ToInt32(dt.Rows[0][0]);

                    ChangeTracker.InsertChange(System.Environment.UserName, DateTime.Now, "tbl_ServiceUser", id, this, ChangeTracker.ChangeType.Insert);

                    ConstructOld();
                }
                else
                {
                    query += "UPDATE tbl_ServiceUser SET";

                    query += " Firstname = @0";
                    query += ",Surname= @1";
                    query += ",idStatus= @2";
                    query += ",PNumber = @3";
                    query += ",dob = @4";
                    query += ",postcode = @5";
                    query += ",add1 = @6";
                    query += ",add2 = @7";

                    query += ",Notes = @8";
                    query += ",contactno_primary = @9";
                    query += ",contactno_secondary = @10";

                    query += ",idGender= @11";

                    query += ",location_longitude = @12";
                    query += ",location_latitude = @13";

                    query += ",medicalinfo = @14";

                    query += ",serviceperiodstart = @15";
                    query += ",serviceperiodweeks = @16";

                    query += ",isNotesImportant = @17";
                    query += " WHERE id = @18";


                    object[] prms = new object[] { firstname, surname,
                                                   Convert.ToInt32(idStatus), PNumber,
                                                   dob_Clean,
                                                   postcode, add1, add2,
                                                   notes,
                                                   contactno_primary, contactno_secondary,
                                                   Convert.ToInt32(idGender),
                                                   LongLatCoords.Longatude, LongLatCoords.Latatude,
                                                   medicalinfo,
                                                   ServiceBegins_Clean, PeriodWeekCount,
                                                   isNotesImportant,
                                                   id };


                    Common.Database.ExecuteDatabaseStatement(query, prms);

                    if (idStatus == Status.Deleted)
                    {
                        ChangeTracker.InsertChange(System.Environment.UserName, DateTime.Now, "tbl_ServiceUser", id, this, ChangeTracker.ChangeType.Delete);
                    }
                    else
                    {
                        ChangeTracker.InsertChange(System.Environment.UserName, DateTime.Now, "tbl_ServiceUser", id, this, ChangeTracker.ChangeType.Update);
                    }

                    this.Old["id"]      = id;
                    this.Old["pnumber"] = PNumber;

                    this.Old["firstname"] = firstname;
                    this.Old["surname"]   = surname;

                    this.Old["idstatus"]            = idStatus;
                    this.Old["idgender"]            = idGender;
                    this.Old["dob"]                 = dob;
                    this.Old["contactno_primary"]   = contactno_primary;
                    this.Old["contactno_secondary"] = contactno_secondary;

                    this.Old["postcode"] = postcode;
                    this.Old["add1"]     = add1;
                    this.Old["add2"]     = add2;

                    this.Old["medicalinfo"]     = medicalinfo;
                    this.Old["notes"]           = notes;
                    this.Old["servicebegins"]   = ServiceBegins;
                    this.Old["periodweekcount"] = PeriodWeekCount;
                    this.Old["LongLatCoords"]   = LongLatCoords;


                    this.OldKeyWorkers.Clear();
                    this.OldKeyWorkers.AddRange(KeyWorkers);
                }


                //Now do the key workers

                query = "DELETE FROM tbl_KeyWorkers WHERE idServiceUser = "******"INSERT INTO tbl_KeyWorkers(idServiceUser, idWorker)";
                    query += "VALUES (@0, @1)";

                    Common.Database.ExecuteDatabaseStatement(query, new object[] { id, worker.id });
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Service User Save Failed", ex);
            }


            MarkForSave = false;
        }