HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Insert values into two different but similar tables

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
tablesinsertintobutdifferenttwovaluessimilar

Problem

I have a combobox with two items. And if one is selected I want to write data to "salary" table and if selected another, to "other" table.

The difference between two code blocks is only one word. I would like to know how I could avoid repeating same code twice.

string myConnection = "datasource=localhost; port=3306; username=root;password=root";
                if (comboBox4.SelectedItem.ToString() == "Salary")
                {
                    string insert = "insert into budget.salary (name, suma) values (@name, @price);";

                    using (var conDataBase = new MySqlConnection(myConnection))
                    using (var cmdDataBase = new MySqlCommand(insert, conDataBase))
                    {
                        cmdDataBase.Parameters.AddWithValue("@name", name);
                        cmdDataBase.Parameters.AddWithValue("@price", suma);
                        conDataBase.Open();
                        cmdDataBase.ExecuteNonQuery();

                        MessageBox.Show("Saved");
                    }
                }
                if (comboBox4.SelectedItem.ToString() == "Other")
                {
                    string insert = "insert into budget.other (name, suma) values (@name, @price);";

                    using (var conDataBase = new MySqlConnection(myConnection))
                    using (var cmdDataBase = new MySqlCommand(insert, conDataBase))
                    {
                        cmdDataBase.Parameters.AddWithValue("@name", name);
                        cmdDataBase.Parameters.AddWithValue("@price", suma);
                        conDataBase.Open();
                        cmdDataBase.ExecuteNonQuery();
                        MessageBox.Show("Saved");

                    }
                }

Solution

Declare a method:

public void InsertInto(string table) {
  string myConnection = "datasource=localhost; port=3306; username=root;password=root";
  string insert = "insert into budget." + table + " (name, suma) values (@name, @price);";

  using (var conDataBase = new MySqlConnection(myConnection))
  using (var cmdDataBase = new MySqlCommand(iterpti, conDataBase)) {
    cmdDataBase.Parameters.AddWithValue("@name", name);
    cmdDataBase.Parameters.AddWithValue("@price", suma);
    conDataBase.Open();
    cmdDataBase.ExecuteNonQuery();
    MessageBox.Show("Saved");
  }
}


And change your code to call the method:

if (comboBox4.SelectedItem.ToString() == "Salary") {
    InsertInto("salary");
}
if (comboBox4.SelectedItem.ToString() == "Other") {
    InsertInto("other");     
}


Declaring a method that does what you want and then calling the method multiple times instead of repeating your code multiple times is the standard way to avoid duplicating code. This principle is known as encapsulation and is very necessary for writing clean, readable, and reusable code. You should get used to using this technique whenever possible.

More info: https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)

Code Snippets

public void InsertInto(string table) {
  string myConnection = "datasource=localhost; port=3306; username=root;password=root";
  string insert = "insert into budget." + table + " (name, suma) values (@name, @price);";

  using (var conDataBase = new MySqlConnection(myConnection))
  using (var cmdDataBase = new MySqlCommand(iterpti, conDataBase)) {
    cmdDataBase.Parameters.AddWithValue("@name", name);
    cmdDataBase.Parameters.AddWithValue("@price", suma);
    conDataBase.Open();
    cmdDataBase.ExecuteNonQuery();
    MessageBox.Show("Saved");
  }
}
if (comboBox4.SelectedItem.ToString() == "Salary") {
    InsertInto("salary");
}
if (comboBox4.SelectedItem.ToString() == "Other") {
    InsertInto("other");     
}

Context

StackExchange Code Review Q#134475, answer score: 4

Revisions (0)

No revisions yet.