// Get records from the table public List <Squats> CmbSQuat() { List <Squats> Squatlists = new List <Squats>(); try { string cmdText = "SELECT * FROM baza1.dbo.Squat"; SqlCommand cmd = new SqlCommand(cmdText, conn); conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Squats s = new Squats(); s.Reps = Convert.ToInt32(reader["Reps"]); s.Set = Convert.ToInt32(reader["Set"]); s.Weights = Convert.ToInt32(reader["Weight"]); Squatlists.Add(s); } return(Squatlists); } catch (Exception) { throw; } finally { if (conn != null) { conn.Close(); } } }
// Insert record into the table and refresh combobox. If table is clear, set identity as 0. private void btnIns_Click(object sender, EventArgs e) { try { if (cmbSquat.Items.Count == 0) { b.ResetInc(); } Squats s = new Squats(); s.Weights = Convert.ToInt32(txtInsW.Text); s.Reps = Convert.ToInt32(txtInsR.Text); b.InsertSquat(s); cmbSquat.DataSource = b.CmbSQuat(); this.squatTableAdapter.Fill(this.baza1DataSet.Squat); textBox1.Text = b.TotalVolume().ToString(); Info(); b.SaveMaxVolume(); b.SaveMaxWeight(); textBox2.Text = b.MaxSquat().ToString(); textBox3.Text = b.GetMaxVolume().ToString(); textBox4.Text = b.GetMaxWeight().ToString(); } catch (Exception) { MessageBox.Show("Please enter a number"); } }
// Update the record in the table public void AddSquat(Squats newSq, Squats oldSq) { try { string cmdText = "UPDATE dbo.Squat SET Weight = @Weight, Reps = @Reps WHERE [Set] = @Set"; SqlCommand cmd = new SqlCommand(cmdText, conn); cmd.Parameters.AddWithValue("@Weight", newSq.Weights); cmd.Parameters.AddWithValue("@Reps", newSq.Reps); cmd.Parameters.AddWithValue("@Set", oldSq.Set); conn.Open(); cmd.ExecuteNonQuery(); } catch (Exception) { throw; } finally { if (conn != null) { conn.Close(); } } }
// Update old record where Id = Id from combobox and refresh private void button1_Click(object sender, EventArgs e) { try { Squats newSq = new Squats(); Squats oldSq = new Squats(); oldSq = cmbSquat.SelectedItem as Squats; newSq.Weights = int.Parse(txtWeight.Text); newSq.Reps = int.Parse(txtReps.Text); b.AddSquat(newSq, oldSq); this.squatTableAdapter.Fill(this.baza1DataSet.Squat); textBox1.Text = b.TotalVolume().ToString(); Info(); b.SaveMaxVolume(); b.SaveMaxWeight(); textBox2.Text = b.MaxSquat().ToString(); textBox3.Text = b.GetMaxVolume().ToString(); textBox4.Text = b.GetMaxWeight().ToString(); } catch (Exception) { MessageBox.Show("Please enter a number"); } }
// Insert record into the table public void InsertSquat(Squats s) { try { string cmdText = "INSERT INTO baza1.dbo.Squat(Weight, Reps) VALUES(@Weight, @Reps)"; SqlCommand cmd = new SqlCommand(cmdText, conn); cmd.Parameters.AddWithValue("@Weight", s.Weights); cmd.Parameters.AddWithValue("@Reps", s.Reps); conn.Open(); cmd.ExecuteNonQuery(); } catch (Exception) { throw; } finally { if (conn != null) { conn.Close(); } } }