private void btnEdit_Click(object sender, EventArgs e) { string dateString, model, full_name, objekt_begin, object_end; string[] vid = new string[4]; String[] kolichestvo_text = new String[4]; string[] merna_edinitsa = new string[4]; decimal begin_km, end_km, km_tovaritelnitsa, km, gorivo; String begin_Km, end_Km, km_Tovaritelnitsa, Km, Gorivo; decimal[] kolichestvo_sum = new decimal[4]; int nomer_paten_list, nomer_tovaritelnitsa, hours_worked; bool[] hasError = new bool[4]; CultureInfo culture = new CultureInfo("bg-BG"); string connectionString = null; SqlConnection conn; connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Transport;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "UPDATE Отчет SET Дата=@Data, МПС=@MPS, Начало_км=@Begin_km, Край_км=@End_km, Км_товарителница=@Km_tovaritelnitsa, Отработени_часове=@Hours_worked, Шофьор=@Shofior, Обект_начало=@Object_begin, Обект_край=@Object_end, Материал_1=@Material_1, Материал_2=@Material_2, Материал_3=@Material_3, Материал_4=@Material_4, Количество_текст_1=@Kolichestvo_text_1, Количество_текст_2=@Kolichestvo_text_2, Количество_текст_3=@Kolichestvo_text_3, Количество_текст_4=@Kolichestvo_text_4, Количество_сума_1=@Kolichestvo_suma_1, Количество_сума_2=@Kolichestvo_suma_2, Количество_сума_3=@Kolichestvo_suma_3, Количество_сума_4=@Kolichestvo_suma_4, Мерна_единица_1=@Merna_edinitsa_1, Мерна_единица_2=@Merna_edinitsa_2, Мерна_единица_3=@Merna_edinitsa_3, Мерна_единица_4=@Merna_edinitsa_4, Км=@Km, Гориво=@Gorivo WHERE (Номер_пътен_лист=@Number_paten_list AND Номер_товарителница=@Number_tovaritelnitsa)"; dateString = dtpDateFrom.Value.ToShortDateString(); DateTime date = DateTime.Parse(dateString, culture); model = cmbMPS.Text; begin_Km = txtBegin.Text; begin_Km = begin_Km.Replace(",", "."); begin_km = decimal.Parse(begin_Km, new CultureInfo("en-US")); end_Km = txtEnd.Text; end_Km = end_Km.Replace(",", "."); end_km = decimal.Parse(end_Km, new CultureInfo("en-US")); km_Tovaritelnitsa = txtKmTovaritelnitsa.Text; km_Tovaritelnitsa = km_Tovaritelnitsa.Replace(",", "."); km_tovaritelnitsa = decimal.Parse(km_Tovaritelnitsa, new CultureInfo("en-US")); hours_worked = int.Parse(txtHoursWorked.Text); full_name = cmbDriver.Text; nomer_paten_list = int.Parse(txtNumber_p_list.Text); nomer_tovaritelnitsa = int.Parse(txtNumber_tovar.Text); objekt_begin = cmbObjectBegin.Text; object_end = cmbObjectEnd.Text; vid[0] = cmbMaterial1.Text; vid[1] = cmbMaterial2.Text; vid[2] = cmbMaterial3.Text; vid[3] = cmbMaterial4.Text; kolichestvo_text[0] = txtKolichestvo1.Text; kolichestvo_text[0] = kolichestvo_text[0].Replace(",", "."); kolichestvo_text[1] = txtKolichestvo2.Text; kolichestvo_text[1] = kolichestvo_text[1].Replace(",", "."); kolichestvo_text[2] = txtKolichestvo3.Text; kolichestvo_text[2] = kolichestvo_text[2].Replace(",", "."); kolichestvo_text[3] = txtKolichestvo4.Text; kolichestvo_text[3] = kolichestvo_text[3].Replace(",", "."); Expression[] expr = new Expression[4]; for (int i = 0; i < 4; i++) { if (kolichestvo_text[i] == "") { kolichestvo_sum[i] = 0; kolichestvo_text[i] = "0"; expr[i] = new Expression(kolichestvo_text[i]); hasError[i] = expr[i].HasErrors(); } else { expr[i] = new Expression(kolichestvo_text[i]); hasError[i] = expr[i].HasErrors(); if (hasError[i] == true) { MessageBox.Show("Въведеният израз е НЕВАЛИДЕН!"); kolichestvo_sum[i] = 0; } else { kolichestvo_sum[i] = Convert.ToDecimal(expr[i].Evaluate()); } } } merna_edinitsa[0] = cmbMerna_edinitsa1.Text; merna_edinitsa[1] = cmbMerna_edinitsa2.Text; merna_edinitsa[2] = cmbMerna_edinitsa3.Text; merna_edinitsa[3] = cmbMerna_edinitsa4.Text; km = end_km - begin_km; Gorivo = txtGorivo.Text; Gorivo = Gorivo.Replace(",", "."); gorivo = decimal.Parse(Gorivo, new CultureInfo("en-US")); cmd.Parameters.AddWithValue("@Data", date); cmd.Parameters.AddWithValue("@MPS", model); cmd.Parameters.AddWithValue("@Begin_km", begin_km); cmd.Parameters.AddWithValue("@End_km", end_km); cmd.Parameters.AddWithValue("@Km_tovaritelnitsa", km_tovaritelnitsa); cmd.Parameters.AddWithValue("@Hours_worked", hours_worked); cmd.Parameters.AddWithValue("@Shofior", full_name); cmd.Parameters.AddWithValue("@Number_paten_list", nomer_paten_list); cmd.Parameters.AddWithValue("@Number_tovaritelnitsa", nomer_tovaritelnitsa); cmd.Parameters.AddWithValue("@Object_begin", objekt_begin); cmd.Parameters.AddWithValue("@Object_end", object_end); cmd.Parameters.AddWithValue("@Material_1", vid[0]); cmd.Parameters.AddWithValue("@Material_2", vid[1]); cmd.Parameters.AddWithValue("@Material_3", vid[2]); cmd.Parameters.AddWithValue("@Material_4", vid[3]); cmd.Parameters.AddWithValue("@Kolichestvo_text_1", kolichestvo_text[0]); cmd.Parameters.AddWithValue("@Kolichestvo_text_2", kolichestvo_text[1]); cmd.Parameters.AddWithValue("@Kolichestvo_text_3", kolichestvo_text[2]); cmd.Parameters.AddWithValue("@Kolichestvo_text_4", kolichestvo_text[3]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_1", kolichestvo_sum[0]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_2", kolichestvo_sum[1]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_3", kolichestvo_sum[2]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_4", kolichestvo_sum[3]); cmd.Parameters.AddWithValue("@Merna_edinitsa_1", merna_edinitsa[0]); cmd.Parameters.AddWithValue("@Merna_edinitsa_2", merna_edinitsa[1]); cmd.Parameters.AddWithValue("@Merna_edinitsa_3", merna_edinitsa[2]); cmd.Parameters.AddWithValue("@Merna_edinitsa_4", merna_edinitsa[3]); cmd.Parameters.AddWithValue("@Km", km); cmd.Parameters.AddWithValue("@Gorivo", gorivo); try { conn.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("Данните са обновени успешно!!!"); cmbMPS.SelectedIndex = -1; txtBegin.Text = null; txtEnd.Text = null; txtKmTovaritelnitsa.Text = null; txtHoursWorked.Text = null; cmbDriver.SelectedIndex = -1; txtNumber_p_list.Text = null; txtNumber_tovar.Text = null; cmbObjectBegin.SelectedIndex = -1; cmbObjectEnd.SelectedIndex = -1; cmbMaterial1.SelectedIndex = -1; cmbMaterial2.SelectedIndex = -1; cmbMaterial3.SelectedIndex = -1; cmbMaterial4.SelectedIndex = -1; txtKolichestvo1.Text = null; txtKolichestvo2.Text = null; txtKolichestvo3.Text = null; txtKolichestvo4.Text = null; cmbMerna_edinitsa1.SelectedIndex = -1; cmbMerna_edinitsa2.SelectedIndex = -1; cmbMerna_edinitsa3.SelectedIndex = -1; cmbMerna_edinitsa4.SelectedIndex = -1; txtGorivo.Text = null; conn.Close(); } catch (Exception ex) { MessageBox.Show("Базата данни не се отваря, защото се опитвате да обновите несъщестуващи данни!"); } }
private void btnAdd_Click(object sender, System.EventArgs e) { string dateString, reg_number, full_name, objectBegin, objectEnd; string[] vid = new string[4]; String[] kolichestvo_text = new String[4]; string[] merna_edinitsa = new string[4]; decimal begin_km, end_km, km_tovaritelnitsa, km, gorivo; String begin_Km, end_Km, km_Tovaritelnitsa, Km, Gorivo; decimal[] kolichestvo_sum = new decimal[4]; int nomer_paten_list, nomer_tovaritelnitsa, hours_worked; bool[] hasError = new bool[4]; CultureInfo culture = new CultureInfo("bg-BG"); string connectionString = null; SqlConnection conn; connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Transport;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "INSERT INTO Отчет values (@Data, @MPS, @Begin_km, @End_km, @Km_tovaritelnitsa, @Hours_worked, @Shofior, @Number_paten_list, @Number_tovaritelnitsa, @ObjectBegin, @ObjectEnd, @Material_1, @Material_2, @Material_3, @Material_4, @Kolichestvo_text_1, @Kolichestvo_text_2, @Kolichestvo_text_3, @Kolichestvo_text_4, @Kolichestvo_suma_1, @Kolichestvo_suma_2, @Kolichestvo_suma_3, @Kolichestvo_suma_4, @Merna_edinitsa_1, @Merna_edinitsa_2, @Merna_edinitsa_3, @Merna_edinitsa_4, @Km, @Gorivo)"; dateString = dtpDateFrom.Value.ToShortDateString(); DateTime date = DateTime.Parse(dateString, culture); reg_number = cmbMPS.Text; if (txtBegin.Text == "") { begin_km = 0; } else { begin_Km = txtBegin.Text; begin_Km = begin_Km.Replace(",", "."); begin_km = decimal.Parse(begin_Km, new CultureInfo("en-US")); } if (txtEnd.Text == "") { end_km = 0; } else { end_Km = txtEnd.Text; end_Km = end_Km.Replace(",", "."); end_km = decimal.Parse(end_Km, new CultureInfo("en-US")); } if (txtKmTovaritelnitsa.Text == "") { km_tovaritelnitsa = 0; } else { km_Tovaritelnitsa = txtKmTovaritelnitsa.Text; km_Tovaritelnitsa = km_Tovaritelnitsa.Replace(",", "."); km_tovaritelnitsa = decimal.Parse(km_Tovaritelnitsa, new CultureInfo("en-US")); } if (txtHoursWorked.Text == "") { hours_worked = 0; } else { hours_worked = int.Parse(txtHoursWorked.Text); } full_name = cmbDriver.Text; nomer_paten_list = int.Parse(txtNumber_p_list.Text); nomer_tovaritelnitsa = int.Parse(txtNumber_tovar.Text); objectBegin = cmbObjectBegin.Text; objectEnd = cmbObjectEnd.Text; vid[0] = cmbMaterial1.Text; vid[1] = cmbMaterial2.Text; vid[2] = cmbMaterial3.Text; vid[3] = cmbMaterial4.Text; kolichestvo_text[0] = txtKolichestvo1.Text; kolichestvo_text[1] = txtKolichestvo2.Text; kolichestvo_text[2] = txtKolichestvo3.Text; kolichestvo_text[3] = txtKolichestvo4.Text; Expression[] expr = new Expression[4]; for (int i = 0; i < 4; i++) { if (kolichestvo_text[i] == "") { kolichestvo_sum[i] = 0; kolichestvo_text[i] = "0"; expr[i] = new Expression(kolichestvo_text[i]); hasError[i] = expr[i].HasErrors(); } else { kolichestvo_text[i] = kolichestvo_text[i].Replace(",", "."); expr[i] = new Expression(kolichestvo_text[i]); hasError[i] = expr[i].HasErrors(); if (hasError[i] == true) { MessageBox.Show("Въведеният израз е НЕВАЛИДЕН!"); kolichestvo_sum[i] = 0; } else { kolichestvo_sum[i] = Convert.ToDecimal(expr[i].Evaluate()); } } } merna_edinitsa[0] = cmbMerna_edinitsa1.Text; merna_edinitsa[1] = cmbMerna_edinitsa2.Text; merna_edinitsa[2] = cmbMerna_edinitsa3.Text; merna_edinitsa[3] = cmbMerna_edinitsa4.Text; km = end_km - begin_km; if (txtGorivo.Text == "") { gorivo = 0; } else { Gorivo = txtGorivo.Text; Gorivo = Gorivo.Replace(",", "."); gorivo = decimal.Parse(Gorivo, new CultureInfo("en-US")); } cmd.Parameters.AddWithValue("@Data", date); cmd.Parameters.AddWithValue("@MPS", reg_number); cmd.Parameters.AddWithValue("@Begin_km", begin_km); cmd.Parameters.AddWithValue("@End_km", end_km); cmd.Parameters.AddWithValue("@Km_tovaritelnitsa", km_tovaritelnitsa); cmd.Parameters.AddWithValue("@Hours_worked", hours_worked); cmd.Parameters.AddWithValue("@Shofior", full_name); cmd.Parameters.AddWithValue("@Number_paten_list", nomer_paten_list); cmd.Parameters.AddWithValue("@Number_tovaritelnitsa", nomer_tovaritelnitsa); cmd.Parameters.AddWithValue("@ObjectBegin", objectBegin); cmd.Parameters.AddWithValue("@ObjectEnd", objectEnd); cmd.Parameters.AddWithValue("@Material_1", vid[0]); cmd.Parameters.AddWithValue("@Material_2", vid[1]); cmd.Parameters.AddWithValue("@Material_3", vid[2]); cmd.Parameters.AddWithValue("@Material_4", vid[3]); cmd.Parameters.AddWithValue("@Kolichestvo_text_1", kolichestvo_text[0]); cmd.Parameters.AddWithValue("@Kolichestvo_text_2", kolichestvo_text[1]); cmd.Parameters.AddWithValue("@Kolichestvo_text_3", kolichestvo_text[2]); cmd.Parameters.AddWithValue("@Kolichestvo_text_4", kolichestvo_text[3]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_1", kolichestvo_sum[0]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_2", kolichestvo_sum[1]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_3", kolichestvo_sum[2]); cmd.Parameters.AddWithValue("@Kolichestvo_suma_4", kolichestvo_sum[3]); cmd.Parameters.AddWithValue("@Merna_edinitsa_1", merna_edinitsa[0]); cmd.Parameters.AddWithValue("@Merna_edinitsa_2", merna_edinitsa[1]); cmd.Parameters.AddWithValue("@Merna_edinitsa_3", merna_edinitsa[2]); cmd.Parameters.AddWithValue("@Merna_edinitsa_4", merna_edinitsa[3]); cmd.Parameters.AddWithValue("@Km", km); cmd.Parameters.AddWithValue("@Gorivo", gorivo); try { conn.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("Данните са въведени успешно!!!"); cmbMPS.SelectedIndex = -1; txtBegin.Text = null; txtEnd.Text = null; txtKmTovaritelnitsa.Text = null; txtHoursWorked.Text = null; cmbDriver.SelectedIndex = -1; txtNumber_p_list.Text = null; txtNumber_tovar.Text = null; cmbObjectBegin.SelectedIndex = -1; cmbObjectEnd.SelectedIndex = -1; cmbMaterial1.SelectedIndex = -1; cmbMaterial2.SelectedIndex = -1; cmbMaterial3.SelectedIndex = -1; cmbMaterial4.SelectedIndex = -1; txtKolichestvo1.Text = null; txtKolichestvo2.Text = null; txtKolichestvo3.Text = null; txtKolichestvo4.Text = null; cmbMerna_edinitsa1.SelectedIndex = -1; cmbMerna_edinitsa2.SelectedIndex = -1; cmbMerna_edinitsa3.SelectedIndex = -1; cmbMerna_edinitsa4.SelectedIndex = -1; txtGorivo.Text = null; conn.Close(); } catch (Exception ex) { MessageBox.Show("Базата данни не се отваря, защото въведените данни съществуват в нея!"); } }