public ShareableProfitView() { InitializeComponent(); ShareableProfit data = new ShareableProfit(); shareableProfit.ItemsSource = data.GetData(); DataContext = data; }
private void Remove_Click(object sender, RoutedEventArgs e) { RemoveDialogView handle = new RemoveDialogView(); if (handle.ShowDialog() == true) { using (SqlConnection con = new SqlConnection(@Connection.ConnectionString)) { if (handle.FirstInput != handle.SecondInput) { MessageBox.Show("Entry No. did not match.Try again.\n", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } Connection conn = new Connection(); conn.OpenConection(); int isLogin = 0; string query = "SELECT * From Stuff "; SqlDataReader reader = conn.DataReader(query); while (reader.Read()) { stuff_name = (string)reader["Stuff_Name"]; stuff_pass = (string)reader["Stuff_Password"]; if (stuff_name.Equals(Login.GlobalStuffName) && stuff_pass.Equals(handle.GetPassword)) { isLogin = 1; break; } } if (isLogin != 1) { MessageBox.Show("Wrong Password.Try again.\n", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } using (SqlCommand command = new SqlCommand("DELETE FROM ShareableProfit WHERE Shareable_Id = " + handle.FirstInput, con)) { con.Open(); command.ExecuteNonQuery(); con.Close(); } Id = Convert.ToInt32(handle.FirstInput); dateTime = DateTime.Today; string table = "ShareableProfit"; string type = "Removed"; string color = "Red"; EntryLog entry = new EntryLog(); entry.Add_Entry(table, type, Id, dateTime, color); conn.CloseConnection(); ShareableProfit data = new ShareableProfit(); shareableProfit.ItemsSource = data.GetData(); DataContext = data; } } }
protected void Save_Click(object sender, RoutedEventArgs e) { if (CheckForError(Deposit) || CheckForError(Expenses)) { MessageBox.Show("Error!Check Input Again"); return; } double previous = this.last_remains(); if ((string)Save.Content == "Save") { using (SqlConnection conn = new SqlConnection(@Connection.ConnectionString)) { SqlCommand CmdSql = new SqlCommand("INSERT INTO [ShareableProfit] (Shareable_Date, Shareable_Previous, Shareable_Deposit, Shareable_Expenses, Shareable_Remains) VALUES (@Date, @Previous, @Deposit, @Expenses, @Remains)", conn); conn.Open(); CmdSql.Parameters.AddWithValue("@Date", Date.SelectedDate); CmdSql.Parameters.AddWithValue("@Previous", previous); CmdSql.Parameters.AddWithValue("@Deposit", Deposit.Text); CmdSql.Parameters.AddWithValue("@Expenses", Expenses.Text); CmdSql.Parameters.AddWithValue("@Remains", previous + Convert.ToDouble(Deposit.Text) - Convert.ToDouble(Expenses.Text)); CmdSql.ExecuteNonQuery(); conn.Close(); //Inserting value in Entry table Connection conn2 = new Connection(); string query = "SELECT TOP 1 * FROM ShareableProfit ORDER BY Shareable_Id DESC"; conn2.OpenConection(); SqlDataReader reader = conn2.DataReader(query); while (reader.Read()) { Id = (int)reader["Shareable_Id"]; dateTime = (DateTime)reader["Shareable_Date"]; } conn2.CloseConnection(); string table = "Shareable Profit"; string type = "Inserted"; string color = "Green"; EntryLog entry = new EntryLog(); entry.Add_Entry(table, type, Id, dateTime, color); } } else { int temp_id = Id; Connection con = new Connection(); string query = "SELECT * FROM ShareableProfit Order by Shareable_Id Asc"; con.OpenConection(); SqlDataReader reader = con.DataReader(query); while (reader.Read()) { string rid = reader["Shareable_Id"].ToString(); int r_id = Convert.ToInt32(rid); string dep = reader["Shareable_Deposit"].ToString(); double depint = Convert.ToDouble(dep); string exp = reader["Shareable_Expenses"].ToString(); double expint = Convert.ToDouble(exp); //code (if block) for updating rest of the table if (temp_id < r_id) { Id = r_id; double prev = this.edited_total(); using (SqlConnection conn = new SqlConnection(@Connection.ConnectionString)) { SqlCommand CmdSql = new SqlCommand("UPDATE [ShareableProfit] SET Shareable_Date = @Date , Shareable_Previous = @Previous, Shareable_Expenses = @Expenses, Shareable_Deposit = @Deposit, Shareable_Remains = @Remains WHERE Shareable_Id=" + r_id, conn); conn.Open(); CmdSql.Parameters.AddWithValue("@Date", Date.SelectedDate); CmdSql.Parameters.AddWithValue("@Previous", prev); CmdSql.Parameters.AddWithValue("@Deposit", dep); CmdSql.Parameters.AddWithValue("@Expenses", exp); CmdSql.Parameters.AddWithValue("@Remains", prev + depint - expint); CmdSql.ExecuteNonQuery(); conn.Close(); } } //Code (else if block) for updating expected row else if (temp_id == r_id) { double prev = this.edited_total(); using (SqlConnection conn = new SqlConnection(@Connection.ConnectionString)) { SqlCommand CmdSql = new SqlCommand("UPDATE [ShareableProfit] SET Shareable_Date = @Date , Shareable_Previous = @Previous, Shareable_Expenses = @Expenses, Shareable_Deposit = @Deposit, Shareable_Remains = @Remains WHERE Shareable_Id=" + EntryNo.Text, conn); conn.Open(); CmdSql.Parameters.AddWithValue("@Date", Date.SelectedDate); CmdSql.Parameters.AddWithValue("@Previous", prev); CmdSql.Parameters.AddWithValue("@Deposit", Deposit.Text); CmdSql.Parameters.AddWithValue("@Expenses", Expenses.Text); CmdSql.Parameters.AddWithValue("@Remains", prev + Convert.ToDouble(Deposit.Text) - Convert.ToDouble(Expenses.Text)); CmdSql.ExecuteNonQuery(); conn.Close(); //Inserting value in Entry table Id = Convert.ToInt32(EntryNo.Text); dateTime = DateTime.Today; string table = "Shareable Profit"; string type = "Updated"; string color = "Blue"; EntryLog entry = new EntryLog(); entry.Add_Entry(table, type, Id, dateTime, color); } Save.Content = "Save"; } } con.CloseConnection(); } ShareableProfit data = new ShareableProfit(); shareableProfit.ItemsSource = data.GetData(); DataContext = data; }