// ADD Record private void buttonADD_Click(object sender, RoutedEventArgs e) { lwdom_LocType_Model ltMod = new lwdom_LocType_Model(); string strMsg = ""; // loads model with data from view ltMod = LoadData_IntoModel(); // display message labelStatus.Content = strMsg; // add the record if (ltMod.LocTypeDesc.Trim() != "") { strMsg = LTWkr.Add_LocType_Rec(ltMod); } else { strMsg = "ADD Canceled. No data entered."; MessageBox.Show(strMsg, "Rate Name", MessageBoxButton.OK, MessageBoxImage.Information); } // display the message labelStatus.Content = strMsg; // list data ListModelsInGrid(); ResetDisplayFields(); InitialButtonConfiguration(); }
// Method: Get List public List<lwdom_LocType_Model> Get_LocType_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, LocTypeDesc " + "FROM lwdom_LocType " + "ORDER BY LocTypeDesc"; // create List List<lwdom_LocType_Model> ltMod_List = new List<lwdom_LocType_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_LocType_Model ltMod = new lwdom_LocType_Model(); ltMod.ID = (reader[0] != DBNull.Value) ? (Int64)reader[0] : 0; ltMod.LocTypeDesc = (reader[1] != DBNull.Value) ? (string)reader[1] : ""; // add Equipment to List ltMod_List.Add(ltMod); } // close reader, close connection reader.Close(); connection.Close(); strMsg = "List Complete."; } catch (Exception errMsg) { strMsg = errMsg.Message.ToString(); } // return List return ltMod_List; }
// load data into the model private lwdom_LocType_Model LoadData_IntoModel() { lwdom_LocType_Model ltMod = new lwdom_LocType_Model(); bool result = false; Int64 intID = 0; // read display fields load data result = Int64.TryParse(lblID.Content.ToString(), out intID); ltMod.ID = (result) ? intID : 0; ltMod.LocTypeDesc = txtLocTypeDesc.Text.Trim(); // return model return ltMod; }
// Display data from grid private void DisplayData(lwdom_LocType_Model ltMod) { lblID.Content = ltMod.ID; txtLocTypeDesc.Text = ltMod.LocTypeDesc; }
// Method: get record data based on id public lwdom_LocType_Model Get_SpecificLocType_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, LocTypeDesc " + "FROM lwdom_LocType " + "WHERE ID=@ID"; // SqlCommand SqlCommand command = new SqlCommand(sqlStatement, connection); // Create object base on LW LocType Model (ltMod) lwdom_LocType_Model ltMod = new lwdom_LocType_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()) { ltMod.ID = (reader[0] != DBNull.Value) ? (Int64)reader[0] : 0; ltMod.LocTypeDesc = (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 ltMod; }
// Method: update record public string Update_LocType_rec(lwdom_LocType_Model ltMod) { // 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_LocType " + "SET LocTypeDesc=@LocTypeDesc " + "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("@LocTypeDesc", ltMod.LocTypeDesc); // command.Parameters.AddWithValue("@ID", ltMod.ID); // must be in the order of the sqlstatement command.ExecuteNonQuery(); strMsg = "Record was updated."; } catch (Exception e) { strMsg = e.Message.ToString(); System.Windows.MessageBox.Show(strMsg, "", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } connection.Close(); return strMsg; }
// ADD public string Add_LocType_Rec(lwdom_LocType_Model ltMod) { // 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_LocType (LocTypeDesc) " + "VALUES (@LocTypeDesc)"; // SqlCommand SqlCommand command = new SqlCommand(sqlStatement, connection); try { connection.Open(); // Adding parameters for the Insert Command command.Parameters.AddWithValue("@LocTypeDesc", ltMod.LocTypeDesc); command.ExecuteNonQuery(); strMsg = "Record was added."; } catch (Exception e) { strMsg = e.Message.ToString(); } connection.Close(); return strMsg; }