internal static void Post2DB() { //---now cycle through the list<> items and parse them and save to DB table. 10/22/2017 // Using a list structure to save time myConn.Close(); //--scratch variables to read each line that starts with X,Y,Z string itemXFromLB = ""; string itemYFromLB = ""; string itemZFromLB = ""; SqlCommand cmd = new SqlCommand("INSERT INTO Accelerometer (xAxis, yAxis,zAxis) VALUES (@xAxis, @yAxis, @zAxis)", myConn); SqlTransaction trans; myConn.Open(); //MAKE SURE THIS IS OPEN FIRST // Must assign both transaction object and connection // to Command object for a pending local transaction trans = myConn.BeginTransaction(); cmd.Transaction = trans; cmd.Connection = myConn; try { foreach (string myItem in General.rawReadings) //10/22/2017 read from list not listbox { if (myItem.Trim().Length > 0) { if (myItem.Substring(0, 1) == "X") { itemXFromLB = myItem.Substring(1); cmd.Parameters.AddWithValue("@xAxis", itemXFromLB); } else if (myItem.Substring(0, 1) == "Y") { itemYFromLB = myItem.Substring(1); cmd.Parameters.AddWithValue("@yAxis", itemYFromLB); } else if (myItem.Substring(0, 1) == "Z") { itemZFromLB = myItem.Substring(1); cmd.Parameters.AddWithValue("@zAxis", itemZFromLB); //---now since we have all three readings, make it so! cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); //use this or it will keep using old parameter values! } } } trans.Commit(); myConn.Close(); General.NotifyUser("Your SQL records have been saved. "); } catch (Exception ex) { General.NotifyUser("An error occurred while saving to database. " + ex.Message.ToString()); try { trans.Rollback(); General.NotifyUser("Your transaction was rolled back."); } catch (Exception ex2) { General.NotifyUser("Unable to rollback the transaction" + ex2.Message.ToString()); myConn.Close(); cmd.Dispose(); } } } #endregion