title: play math with sql commands
body: <p>i'm working on restaurants programming with c#<br>
  working on the daily quantity of the material<br>
  i do count of the total that they make on the day then count how many they used and how many remaining on that day<br>
  my code is   </p>
  
  <pre><code> public void ExecuteQuery(string query)
          {
              foreach (Control txt in this.Controls)
              {
                  if (txt.GetType() == typeof(TextBox))
                      if (string.IsNullOrEmpty(txt.Text))
                          txt.Text = "0";
              }
  
              SqlCommand com = new SqlCommand(query, connection);
              com.Parameters.Add("@shish_add", SqlDbType.Int).Value = shish_add.Text;
              com.Parameters.Add("@bani_add", SqlDbType.Int).Value = bani_add.Text;
              com.Parameters.Add("@zingr_add", SqlDbType.Int).Value = zingr_add.Text;
              com.Parameters.Add("@tabona_add", SqlDbType.Int).Value = tabona_add.Text;
              com.Parameters.Add("@fahita_chkn_add", SqlDbType.Int).Value = fahita_chkn_add.Text;
              com.Parameters.Add("@borger_add", SqlDbType.Int).Value = borger_add.Text;
              com.Parameters.Add("@kabab_add", SqlDbType.Int).Value = kabab_add.Text;
              com.Parameters.Add("@shgef_add", SqlDbType.Int).Value = shgef_add.Text;
              com.Parameters.Add("@faheta_met_add", SqlDbType.Int).Value = faheta_met_add.Text;
              com.Parameters.Add("@kabda_add", SqlDbType.Int).Value = kabda_add.Text;
              com.Parameters.Add("@shawo_met_add", SqlDbType.Int).Value = shawo_met_add.Text;
              com.Parameters.Add("@shawo_chkn_add", SqlDbType.Int).Value = shawo_chkn_add.Text;
              com.Parameters.Add("@chkn_mash_add", SqlDbType.Int).Value = chkn_mash_add.Text;
              com.Parameters.Add("@chkn_red_add", SqlDbType.Int).Value = chkn_red_add.Text;
              com.Parameters.Add("@patata_add", SqlDbType.Int).Value = patata_add.Text;
  
              com.Parameters.Add("@shish_take", SqlDbType.Int).Value = shish_take.Text;
              com.Parameters.Add("@bani_take", SqlDbType.Int).Value = bani_take.Text;
              com.Parameters.Add("@zingr_take", SqlDbType.Int).Value = zingr_take.Text;
              com.Parameters.Add("@tabona_take", SqlDbType.Int).Value = tabona_take.Text;
              com.Parameters.Add("@fahita_chkn_take", SqlDbType.Int).Value = fahita_chkn_take.Text;
              com.Parameters.Add("@borger_take", SqlDbType.Int).Value = borger_take.Text;
              com.Parameters.Add("@kabab_take", SqlDbType.Int).Value = kabab_take.Text;
              com.Parameters.Add("@shgef_take", SqlDbType.Int).Value = shgef_take.Text;
              com.Parameters.Add("@faheta_met_take", SqlDbType.Int).Value = faheta_met_take.Text;
              com.Parameters.Add("@kabda_take", SqlDbType.Int).Value = kabda_take.Text;
              com.Parameters.Add("@shawo_met_take", SqlDbType.Int).Value = shawo_met_take.Text;
              com.Parameters.Add("@shawo_chkn_take", SqlDbType.Int).Value = shawo_chkn_take.Text;
              com.Parameters.Add("@chkn_mash_take", SqlDbType.Int).Value = chkn_mash_take.Text;
              com.Parameters.Add("@chkn_red_take", SqlDbType.Int).Value = chkn_red_take.Text;
              com.Parameters.Add("@patata_take", SqlDbType.Int).Value = patata_take.Text;
  
              com.Parameters.Add("@shish_rom", SqlDbType.Int).Value = shish_rom.Text;
              com.Parameters.Add("@bani_rom", SqlDbType.Int).Value = bani_rom.Text;
              com.Parameters.Add("@zingr_rom", SqlDbType.Int).Value = zingr_rom.Text;
              com.Parameters.Add("@tabona_rom", SqlDbType.Int).Value = tabona_rom.Text;
              com.Parameters.Add("@fahita_chkn_rom", SqlDbType.Int).Value = fahita_chkn_rom.Text;
              com.Parameters.Add("@borger_rom", SqlDbType.Int).Value = borger_rom.Text;
              com.Parameters.Add("@kabab_rom", SqlDbType.Int).Value = kabab_rom.Text;
              com.Parameters.Add("@shgef_rom", SqlDbType.Int).Value = shgef_rom.Text;
              com.Parameters.Add("@faheta_met_rom", SqlDbType.Int).Value = faheta_met_rom.Text;
              com.Parameters.Add("@kabda_rom", SqlDbType.Int).Value = kabda_rom.Text;
              com.Parameters.Add("@shawo_met_rom", SqlDbType.Int).Value = shawo_met_rom.Text;
              com.Parameters.Add("@shawo_chkn_rom", SqlDbType.Int).Value = shawo_chkn_rom.Text;
              com.Parameters.Add("@chkn_mash_rom", SqlDbType.Int).Value = chkn_mash_rom.Text;
              com.Parameters.Add("@chkn_red_rom", SqlDbType.Int).Value = chkn_red_rom.Text;
              com.Parameters.Add("@patata_rom", SqlDbType.Int).Value = patata_rom.Text;
  
              com.Parameters.Add("@dates", SqlDbType.DateTime).Value = dates.Value.Date;
  
  
              if (connection.State == ConnectionState.Closed)
                  connection.Open();
  
              com.ExecuteNonQuery();
  
              connection.Close();
  
              foreach (Control txt in this.Controls)
              {
                  if (txt.GetType() == typeof(TextBox))
                      if (txt.Text == "0")
                          txt.Text = "";
              }
              MessageBox.Show("تمت عملية الاضافة بنجاح", "ملاحظة", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
  
          }
          public void doqury(string strn)
          {
              string sqlQuery = strn;
               ExecuteQuery(sqlQuery);
          }
  
          private void button1_Click(object sender, EventArgs e)
  
          {
              // check for first time entry 
              SqlCommand cmd = new SqlCommand("select * from megana_cooking ", connection);
              connection.Open();
              SqlDataReader reader = cmd.ExecuteReader();
  
              if (reader.HasRows)
              {
                  //mean there values on database
                  //now need to check is this a new day or not
                  SqlCommand cmd1 = new SqlCommand("select * from megana_cooking where dates = @dates", connection);
                  connection.Open();
                  SqlDataReader reader1 = cmd.ExecuteReader();
                  if (reader1.HasRows)
                  {
                      //no this is the take valus statment
                  reader.Close();
                  doqury("UPDATE megana_cooking SET  shish_rom =shish_add - @shish_take ,bani_rom =bani_add - @bani_take ,zingr_rom =zingr_add - @zingr_take ,tabona_rom =tabona_add - @tabona_take ,fahita_chkn_rom =fahita_chkn_add - @fahita_chkn_take ,borger_rom =borger_add - @borger_take ,kabab_rom =kabab_add - @kabab_take ,shgef_rom =shgef_add - @shgef_take ,faheta_met_rom =faheta_met_add - @faheta_met_take ,kabda_rom =kabda_add - @kabda_take ,shawo_met_rom =shawo_met_add - @shawo_met_take ,shawo_chkn_rom =shawo_chkn_add - @shawo_chkn_take ,chkn_mash_rom =chkn_mash_add - @chkn_mash_take ,chkn_red_rom =chkn_red_add - @chkn_red_take ,patata_rom =patata_add - @patata_take ,patata_take =@patata_take,chkn_red_take =@chkn_red_take,chkn_mash_take =@chkn_mash_take,shawo_chkn_take =@shawo_chkn_take,shawo_met_take =@shawo_met_take,kabda_take =@kabda_take,faheta_met_take =@faheta_met_take,shgef_take =@shgef_take,kabab_take =@kabab_take,borger_take =@borger_take,fahita_chkn_take =@fahita_chkn_take,tabona_take =@tabona_take,zingr_take =@zingr_take,bani_take =@bani_take,shish_take =@shish_take ,dates =@dates  where dates = @dates ");
  
                  }
  
                  else
                  {
                      // yes new day new values and add value to the remain values from last entry 
                      DialogResult result = MessageBox.Show("سيتم ادخال بيانات ليوم جديد " + Environment.NewLine + " سيقوم بأدخال البيانات دون احتساب عملية السحب", "تأكيد", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1,
                       MessageBoxOptions.RightAlign);
                      doqury("INSERT INTO megana_cooking ( shish_add ,bani_add ,zingr_add ,tabona_add ,fahita_chkn_add ,borger_add ,kabab_add ,shgef_add ,faheta_met_add ,kabda_add ,shawo_met_add ,shawo_chkn_add ,chkn_mash_add ,chkn_red_add ,patata_add ,dates ) VALUES ( @shish_add ,@bani_add ,@zingr_add ,@tabona_add ,@fahita_chkn_add ,@borger_add ,@kabab_add ,@shgef_add ,@faheta_met_add ,@kabda_add ,@shawo_met_add ,@shawo_chkn_add ,@chkn_mash_add ,@chkn_red_add ,@patata_add ,@dates )");
                      //insert new quentity done now need to update the remain value 
                      //new quentity + last remain 
  
  here need to find the last date that it have value then update and add the new quantity to it 
  
  
                  }
  
  
              }
              else
              {
                  //this is first entry
                  reader.Close();
  
                  DialogResult result = MessageBox.Show("سيتم ادخال البيانات الى المنظومة لاول مره " + Environment.NewLine + " سيقوم بأدخال البيانات دون احتساب عملية السحب", "تأكيد", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RightAlign);
  
                  doqury("INSERT INTO megana_cooking ( shish_add ,bani_add ,zingr_add ,tabona_add ,fahita_chkn_add ,borger_add ,kabab_add ,shgef_add ,faheta_met_add ,kabda_add ,shawo_met_add ,shawo_chkn_add ,chkn_mash_add ,chkn_red_add ,patata_add ,shish_rom ,bani_rom ,zingr_rom ,tabona_rom ,fahita_chkn_rom ,borger_rom ,kabab_rom ,shgef_rom ,faheta_met_rom ,kabda_rom ,shawo_met_rom ,shawo_chkn_rom ,chkn_mash_rom ,chkn_red_rom ,patata_rom ,dates ) VALUES ( @shish_add ,@bani_add ,@zingr_add ,@tabona_add ,@fahita_chkn_add ,@borger_add ,@kabab_add ,@shgef_add ,@faheta_met_add ,@kabda_add ,@shawo_met_add ,@shawo_chkn_add ,@chkn_mash_add ,@chkn_red_add ,@patata_add ,@shish_add ,@bani_add ,@zingr_add ,@tabona_add ,@fahita_chkn_add ,@borger_add ,@kabab_add ,@shgef_add ,@faheta_met_add ,@kabda_add ,@shawo_met_add ,@shawo_chkn_add ,@chkn_mash_add ,@chkn_red_add ,@patata_add ,@dates )");
  
  
              }
              connection.Close();
  
  
          }
  </code></pre>
  
  <p>the question is<br>
  i need to find the last date that it have value then update and add the new quantity to it i did mention it in my code<br>
  i know i can because when i tried this code on new query on sql management studio it work </p>
  
  <pre><code>SELECT 
     *
  FROM 
      megana_cooking
  ORDER BY 
      dates
      update megana_cooking set shish_rom = shish_rom + '5'
  </code></pre>
  
  <p>can i make the sql command shorter than this by using sql programmability or server side because i have more than 15 type of the material food<br>
  i have weak knowledge about sql<br>
  thank you in advance</p>