// ADD record private void buttonADD_Click(object sender, RoutedEventArgs e) { string strMsg = ""; lwdom_RatesID_Model lwridMod = new lwdom_RatesID_Model(); // loads model with data from view lwridMod = LoadRatesID_Model(); // add the record if (lwridMod.RateName.Trim() != "") { strMsg = LWRIDWkr.Add_RatesID_Rec(lwridMod); } else { strMsg = "ADD Canceled. No data entered."; MessageBox.Show(strMsg, "Rate Name", MessageBoxButton.OK, MessageBoxImage.Information); } // display the message labelStatus.Content = strMsg; // list data ListModelInGrid(); ResetDisplayFields(); InitialButtonConfiguration(); }
// Method: Get List public List<lwdom_RatesID_Model> Get_RatesID_List() { // building the connection string // get the provider, activeStatus database name, and path connectionString = PSWkr.G_SQLDatabaseConnectionString; string strMsg = ""; // create needed objects SqlConnection connection; // building sql command, chemcode is the Key string sqlStatement = "SELECT ID, RateName " + "FROM lwdom_RatesID " + "ORDER BY RateName"; // create List List<lwdom_RatesID_Model> lwridMod_List = new List<lwdom_RatesID_Model>(); try { connection = new SqlConnection(connectionString); connection.Open(); SqlCommand command = new SqlCommand(sqlStatement, connection); SqlDataReader reader = command.ExecuteReader(); // read table, populate model while (reader.Read()) { lwdom_RatesID_Model lwridMod = new lwdom_RatesID_Model(); lwridMod.ID = (reader[0] != DBNull.Value) ? (Int64)reader[0] : 0; lwridMod.RateName = (reader[1] != DBNull.Value) ? (string)reader[1] : ""; // add Equipment to List lwridMod_List.Add(lwridMod); } // close reader, close connection reader.Close(); connection.Close(); strMsg = "List Complete."; } catch (Exception errMsg) { strMsg = errMsg.Message.ToString(); } // return List return lwridMod_List; }
// UPDATE private void buttonUpdate_Click(object sender, RoutedEventArgs e) { string strMsg = ""; lwdom_RatesID_Model lwridMod = new lwdom_RatesID_Model(); // loads model with data from view lwridMod = LoadRatesID_Model(); // add the record strMsg = LWRIDWkr.Update_RatesID_rec(lwridMod); // display the message labelStatus.Content = strMsg; // list data ListModelInGrid(); ResetDisplayFields(); InitialButtonConfiguration(); }
// load data model with data from window private lwdom_RatesID_Model LoadRatesID_Model() { int _id = 0; bool result = false; lwdom_RatesID_Model lwridMod = new lwdom_RatesID_Model(); result = int.TryParse(lblID.Content.ToString(), out _id); lwridMod.ID = (result == true) ? _id : 0; lwridMod.RateName = txtRateName.Text.Trim(); // return model return lwridMod; }
// display the data private void DisplayData(lwdom_RatesID_Model lwridMod) { lblID.Content = lwridMod.ID.ToString(); txtRateName.Text = lwridMod.RateName; }
// Method: get record data based on id public lwdom_RatesID_Model Get_SpecificRatesID_Record(int recID) { string strMsg = ""; // get the connection string connectionString = PSWkr.G_SQLDatabaseConnectionString; // create connection object SqlConnection connection = new SqlConnection(connectionString); // building sql command string sqlStatement = "SELECT ID, RateName " + "FROM lwdom_RatesID " + "WHERE ID=@ID"; // SqlCommand SqlCommand command = new SqlCommand(sqlStatement, connection); // Create object base on LW Rates Model (lwrMod) lwdom_RatesID_Model lwridMod = new lwdom_RatesID_Model(); try { // open the connection connection.Open(); command.Parameters.AddWithValue("@ID", recID); // execute the reader SqlDataReader reader = command.ExecuteReader(); // populate the invoice list if (reader.Read()) { lwridMod.ID = (reader[0] != DBNull.Value) ? (Int64)reader[0] : 0; lwridMod.RateName = (reader[1] != DBNull.Value) ? (string)reader[1] : ""; } // the close reader.Close(); } catch (Exception e) { strMsg = e.Message.ToString(); } // the close connection.Close(); // return the Model return lwridMod; }
// Reset lwdom_Rates Model private lwdom_RatesID_Model Reset_RatesID_Model(lwdom_RatesID_Model lwridMod) { // reset the model lwridMod.ID = 0; lwridMod.RateName = ""; // return the model return lwridMod; }
// Method: update record public string Update_RatesID_rec(lwdom_RatesID_Model lwridMod) { // Method: update selected Hours Master recore // update the database string strMsg = ""; // get the connection string connectionString = PSWkr.G_SQLDatabaseConnectionString; // create connection object SqlConnection connection = new SqlConnection(connectionString); // building sql command string sqlStatement = "UPDATE lwdom_RatesID " + "SET RateName=@RateName " + "WHERE ID=@ID"; // SqlCommand SqlCommand command = new SqlCommand(sqlStatement, connection); try { // update the database connection.Open(); // use of command.parameters... prevents sql injection command.Parameters.AddWithValue("@RateName", lwridMod.RateName); // command.Parameters.AddWithValue("@ID", lwridMod.ID); // must be in the order of the sqlstatement command.ExecuteNonQuery(); strMsg = "Record was updated."; } catch (Exception e) { strMsg = e.Message.ToString(); } connection.Close(); return strMsg; }
// ADD public string Add_RatesID_Rec(lwdom_RatesID_Model lwridMod) { // Method: Create new record // update the database string strMsg = ""; // get the connection string connectionString = PSWkr.G_SQLDatabaseConnectionString; // create connection object SqlConnection connection = new SqlConnection(connectionString); // building sql command string sqlStatement = "INSERT INTO lwdom_RatesID (RateName) " + "VALUES (@RateName)"; // SqlCommand SqlCommand command = new SqlCommand(sqlStatement, connection); try { connection.Open(); // Adding parameters for the Insert Command command.Parameters.AddWithValue("@RateName", lwridMod.RateName); command.ExecuteNonQuery(); strMsg = "Record was added."; } catch (Exception e) { strMsg = e.Message.ToString(); } connection.Close(); return strMsg; }