0

I'll cut to the chase - as part of my application, a user may edit alarm ranges for a patient's bed (Min/Max heart rate, etc) - these alarm range changes are saved to a bed class instance (Responsible for holding this data, along with information like bedID from a database - used for identification). The bed class instances are created on startup: one instance to each bed entry in the relevant database table.

The database itself is split into three tables - the table "listOfBeds" being the target. The string "SQL_GETBEDINFO" returns all records from the "listOfBeds" table ("SELECT * FROM dbo.listOfBeds).

The general flow of the process is that the user edits the alarm ranges for the currently selected bed (no issue), the selected bed instance's values are updated upon the user pressing an "apply" button (no issue), the user may then click to save and exit the application (whereupon the changes made to each bed's alarm ranges are uploaded to the database and recalled for next launch): the latter being the issue. Whilst the application reports saving correctly, upon reloading the changes don't appear to have been made - as if they were never sa

Below I've pasted the code for saving the changes to the database; I'm happy to include additional detail as needed - if anybody could point out where I've gone wrong it would be greatly appreciated! `

   public void SaveDataSet(MainForm mainFormReference)
    {
        //Creates a new dataset called "dataSet", this will contain information from the database according to the query we use, then opens connection
        System.Data.DataSet dataSet;
        openConnection();

        //Creates data adapter using database connection and select all records from listOfBeds
        dbDataAdapter = new System.Data.SqlClient.SqlDataAdapter(SQLCommands.SQL_GETBEDINFO, dbConnection);

        //Creates the dataset ready to be filled with data
        dataSet = new System.Data.DataSet();

        //Fills the dataset with information grabbed by the data adapter
        dbDataAdapter.Fill(dataSet);

        try
        {
            //Get table from dataset, get number of rows in table
            DataTable dtBedInfo = dataSet.Tables[0];
            int count = dtBedInfo.Rows.Count;

            //For each bed in the list of bed instances, do the following
            foreach (Bed bed in mainFormReference.listOfBeds)
            {
                //For each row in the datatable
                for (int i = 0; i < count; i++)
                {
                    //If bed instance ID and bedID in row match
                    if (Convert.ToInt32(dtBedInfo.Rows[i]["bedID"]) == bed.bedInstanceID)
                    {
                        //Assign new values from bed instance to datatable row
                        dtBedInfo.Rows[i]["bedMinHeartRate"] = bed.bedInstanceMinHeartRate;
                        dtBedInfo.Rows[i]["bedMaxHeartRate"] = bed.bedInstanceMaxHeartRate;
                        dtBedInfo.Rows[i]["bedMinBreathRate"] = bed.bedInstanceMinBreathRate;
                        dtBedInfo.Rows[i]["bedMaxBreathRate"] = bed.bedInstanceMaxBreathRate;
                        dtBedInfo.Rows[i]["bedMinSysBloodPress"] = bed.bedInstanceMinSysBloodPress;
                        dtBedInfo.Rows[i]["bedMaxSysBloodPress"] = bed.bedInstanceMaxSysBloodPress;
                        dtBedInfo.Rows[i]["bedMinDiaBloodPress"] = bed.bedInstanceMinDiaBloodPress;
                        dtBedInfo.Rows[i]["bedMaxDiaBloodPress"] = bed.bedInstanceMaxDiaBloodPress;
                        dtBedInfo.Rows[i]["bedMinBodyTemp"] = bed.bedInstanceMinBodyTemp;
                        dtBedInfo.Rows[i]["bedMaxBodyTemp"] = bed.bedInstanceMaxBodyTemp;
                    }
                }
            }
            //Do update
            System.Data.SqlClient.SqlCommandBuilder builder = new System.Data.SqlClient.SqlCommandBuilder(dbDataAdapter);
            dbDataAdapter.UpdateCommand = builder.GetUpdateCommand();
            dbDataAdapter.TableMappings.Add("listOfBeds", "0");
            dbDataAdapter.Update(dataSet);
            MessageBox.Show("Save successful!");
            closeConnection();
        }
        catch (Exception Excep)
        {
            //Throw exception
            MessageBox.Show("Save failed! Error:\n" + Excep);
        }
    }
     `
  • What is the connection string? Do you have your database file listed between your project elements? Look at http://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails/17147460#17147460 – Steve Nov 18 '15 at 18:12
  • Solved now! Had the database file itself to copy always: copy if newer and the save function is working perfectly! Thanks so much! – Blizzardsev Nov 18 '15 at 18:21

0 Answers0