Advertisement
antisa

bazeLV7

May 23rd, 2024
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.47 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Windows.Forms;
  5.  
  6. public partial class Form1 : Form
  7. {
  8.     private int? selectedPersonId = null;
  9.  
  10.     public Form1()
  11.     {
  12.         InitializeComponent();
  13.         this.dgvPodaci.SelectionChanged += new System.EventHandler(this.dgvPodaci_SelectionChanged);
  14.         this.btnBrisi.Click += new System.EventHandler(this.btnBrisi_Click);
  15.         this.btnPrikaziSve.Click += new System.EventHandler(this.btnPrikaziSve_Click);
  16.         this.btnSpremi.Click += new System.EventHandler(this.btnSpremi_Click);
  17.     }
  18.  
  19.     private void PrikaziSveOsobe()
  20.     {
  21.         string connectionString = "Server=xxx.xxx.xxx.xxx;Database=student;User Id=student;Password=student;";
  22.         string query = "SELECT * FROM osobe ORDER BY prezime";
  23.  
  24.         using (SqlConnection connection = new SqlConnection(connectionString))
  25.         {
  26.             SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection);
  27.             DataTable dataTable = new DataTable();
  28.             dataAdapter.Fill(dataTable);
  29.             dgvPodaci.DataSource = dataTable;
  30.         }
  31.     }
  32.  
  33.     private void dgvPodaci_SelectionChanged(object sender, EventArgs e)
  34.     {
  35.         if (dgvPodaci.SelectedRows.Count > 0)
  36.         {
  37.             DataGridViewRow selectedRow = dgvPodaci.SelectedRows[0];
  38.             selectedPersonId = Convert.ToInt32(selectedRow.Cells["id"].Value);
  39.             txtOIB.Text = selectedRow.Cells["OIB"].Value.ToString();
  40.             txtIme.Text = selectedRow.Cells["ime"].Value.ToString();
  41.             txtPrezime.Text = selectedRow.Cells["prezime"].Value.ToString();
  42.             txtDatum.Text = Convert.ToDateTime(selectedRow.Cells["datum_rodenja"].Value).ToString("dd.MM.yyyy");
  43.             if (selectedRow.Cells["spol"].Value.ToString() == "M")
  44.             {
  45.                 rbM.Checked = true;
  46.             }
  47.             else
  48.             {
  49.                 rbZ.Checked = true;
  50.             }
  51.             txtVisina.Text = selectedRow.Cells["visina"].Value.ToString();
  52.             txtBrCip.Text = selectedRow.Cells["broj_cipela"].Value.ToString();
  53.  
  54.             btnBrisi.Enabled = true;
  55.         }
  56.         else
  57.         {
  58.             btnBrisi.Enabled = false;
  59.         }
  60.     }
  61.  
  62.     private void SpremiOsobu()
  63.     {
  64.         string connectionString = "Server=xxx.xxx.xxx.xxx;Database=student;User Id=student;Password=student;";
  65.         string query;
  66.  
  67.         if (selectedPersonId == null)
  68.         {
  69.             query = "INSERT INTO osobe (OIB, ime, prezime, datum_rodenja, spol, visina, broj_cipela) " +
  70.                     "VALUES (@OIB, @Ime, @Prezime, @DatumRodenja, @Spol, @Visina, @BrojCipela)";
  71.         }
  72.         else
  73.         {
  74.             query = "UPDATE osobe SET OIB = @OIB, ime = @Ime, prezime = @Prezime, datum_rodenja = @DatumRodenja, " +
  75.                     "spol = @Spol, visina = @Visina, broj_cipela = @BrojCipela WHERE id = @Id";
  76.         }
  77.  
  78.         using (SqlConnection connection = new SqlConnection(connectionString))
  79.         {
  80.             using (SqlCommand command = new SqlCommand(query, connection))
  81.             {
  82.                 command.Parameters.AddWithValue("@OIB", txtOIB.Text);
  83.                 command.Parameters.AddWithValue("@Ime", txtIme.Text);
  84.                 command.Parameters.AddWithValue("@Prezime", txtPrezime.Text);
  85.                 command.Parameters.AddWithValue("@DatumRodenja", DateTime.Parse(txtDatum.Text));
  86.                 command.Parameters.AddWithValue("@Spol", rbM.Checked ? "M" : "Z");
  87.                 command.Parameters.AddWithValue("@Visina", int.Parse(txtVisina.Text));
  88.                 command.Parameters.AddWithValue("@BrojCipela", int.Parse(txtBrCip.Text));
  89.  
  90.                 if (selectedPersonId != null)
  91.                 {
  92.                     command.Parameters.AddWithValue("@Id", selectedPersonId);
  93.                 }
  94.  
  95.                 connection.Open();
  96.                 command.ExecuteNonQuery();
  97.             }
  98.         }
  99.  
  100.         selectedPersonId = null;
  101.         ClearFormFields();
  102.         btnBrisi.Enabled = false;
  103.         PrikaziSveOsobe();
  104.     }
  105.  
  106.     private void BrisiOsobu()
  107.     {
  108.         if (selectedPersonId == null)
  109.         {
  110.             MessageBox.Show("Nije odabrana nijedna osoba za brisanje.");
  111.             return;
  112.         }
  113.  
  114.         string connectionString = "Server=xxx.xxx.xxx.xxx;Database=student;User Id=student;Password=student;";
  115.         string query = "DELETE FROM osobe WHERE id = @Id";
  116.  
  117.         using (SqlConnection connection = new SqlConnection(connectionString))
  118.         {
  119.             using (SqlCommand command = new SqlCommand(query, connection))
  120.             {
  121.                 command.Parameters.AddWithValue("@Id", selectedPersonId);
  122.  
  123.                 connection.Open();
  124.                 command.ExecuteNonQuery();
  125.             }
  126.         }
  127.  
  128.         selectedPersonId = null;
  129.         ClearFormFields();
  130.         btnBrisi.Enabled = false;
  131.         PrikaziSveOsobe();
  132.     }
  133.  
  134.     private void ClearFormFields()
  135.     {
  136.         txtOIB.Clear();
  137.         txtIme.Clear();
  138.         txtPrezime.Clear();
  139.         txtDatum.Clear();
  140.         rbM.Checked = false;
  141.         rbZ.Checked = false;
  142.         txtVisina.Clear();
  143.         txtBrCip.Clear();
  144.     }
  145.  
  146.     private void btnPrikaziSve_Click(object sender, EventArgs e)
  147.     {
  148.         PrikaziSveOsobe();
  149.     }
  150.  
  151.     private void btnSpremi_Click(object sender, EventArgs e)
  152.     {
  153.         SpremiOsobu();
  154.     }
  155.  
  156.     private void btnBrisi_Click(object sender, EventArgs e)
  157.     {
  158.         BrisiOsobu();
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement