Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- public partial class Form1 : Form
- {
- private int? selectedPersonId = null;
- public Form1()
- {
- InitializeComponent();
- this.dgvPodaci.SelectionChanged += new System.EventHandler(this.dgvPodaci_SelectionChanged);
- this.btnBrisi.Click += new System.EventHandler(this.btnBrisi_Click);
- this.btnPrikaziSve.Click += new System.EventHandler(this.btnPrikaziSve_Click);
- this.btnSpremi.Click += new System.EventHandler(this.btnSpremi_Click);
- }
- private void PrikaziSveOsobe()
- {
- string connectionString = "Server=xxx.xxx.xxx.xxx;Database=student;User Id=student;Password=student;";
- string query = "SELECT * FROM osobe ORDER BY prezime";
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection);
- DataTable dataTable = new DataTable();
- dataAdapter.Fill(dataTable);
- dgvPodaci.DataSource = dataTable;
- }
- }
- private void dgvPodaci_SelectionChanged(object sender, EventArgs e)
- {
- if (dgvPodaci.SelectedRows.Count > 0)
- {
- DataGridViewRow selectedRow = dgvPodaci.SelectedRows[0];
- selectedPersonId = Convert.ToInt32(selectedRow.Cells["id"].Value);
- txtOIB.Text = selectedRow.Cells["OIB"].Value.ToString();
- txtIme.Text = selectedRow.Cells["ime"].Value.ToString();
- txtPrezime.Text = selectedRow.Cells["prezime"].Value.ToString();
- txtDatum.Text = Convert.ToDateTime(selectedRow.Cells["datum_rodenja"].Value).ToString("dd.MM.yyyy");
- if (selectedRow.Cells["spol"].Value.ToString() == "M")
- {
- rbM.Checked = true;
- }
- else
- {
- rbZ.Checked = true;
- }
- txtVisina.Text = selectedRow.Cells["visina"].Value.ToString();
- txtBrCip.Text = selectedRow.Cells["broj_cipela"].Value.ToString();
- btnBrisi.Enabled = true;
- }
- else
- {
- btnBrisi.Enabled = false;
- }
- }
- private void SpremiOsobu()
- {
- string connectionString = "Server=xxx.xxx.xxx.xxx;Database=student;User Id=student;Password=student;";
- string query;
- if (selectedPersonId == null)
- {
- query = "INSERT INTO osobe (OIB, ime, prezime, datum_rodenja, spol, visina, broj_cipela) " +
- "VALUES (@OIB, @Ime, @Prezime, @DatumRodenja, @Spol, @Visina, @BrojCipela)";
- }
- else
- {
- query = "UPDATE osobe SET OIB = @OIB, ime = @Ime, prezime = @Prezime, datum_rodenja = @DatumRodenja, " +
- "spol = @Spol, visina = @Visina, broj_cipela = @BrojCipela WHERE id = @Id";
- }
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand command = new SqlCommand(query, connection))
- {
- command.Parameters.AddWithValue("@OIB", txtOIB.Text);
- command.Parameters.AddWithValue("@Ime", txtIme.Text);
- command.Parameters.AddWithValue("@Prezime", txtPrezime.Text);
- command.Parameters.AddWithValue("@DatumRodenja", DateTime.Parse(txtDatum.Text));
- command.Parameters.AddWithValue("@Spol", rbM.Checked ? "M" : "Z");
- command.Parameters.AddWithValue("@Visina", int.Parse(txtVisina.Text));
- command.Parameters.AddWithValue("@BrojCipela", int.Parse(txtBrCip.Text));
- if (selectedPersonId != null)
- {
- command.Parameters.AddWithValue("@Id", selectedPersonId);
- }
- connection.Open();
- command.ExecuteNonQuery();
- }
- }
- selectedPersonId = null;
- ClearFormFields();
- btnBrisi.Enabled = false;
- PrikaziSveOsobe();
- }
- private void BrisiOsobu()
- {
- if (selectedPersonId == null)
- {
- MessageBox.Show("Nije odabrana nijedna osoba za brisanje.");
- return;
- }
- string connectionString = "Server=xxx.xxx.xxx.xxx;Database=student;User Id=student;Password=student;";
- string query = "DELETE FROM osobe WHERE id = @Id";
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand command = new SqlCommand(query, connection))
- {
- command.Parameters.AddWithValue("@Id", selectedPersonId);
- connection.Open();
- command.ExecuteNonQuery();
- }
- }
- selectedPersonId = null;
- ClearFormFields();
- btnBrisi.Enabled = false;
- PrikaziSveOsobe();
- }
- private void ClearFormFields()
- {
- txtOIB.Clear();
- txtIme.Clear();
- txtPrezime.Clear();
- txtDatum.Clear();
- rbM.Checked = false;
- rbZ.Checked = false;
- txtVisina.Clear();
- txtBrCip.Clear();
- }
- private void btnPrikaziSve_Click(object sender, EventArgs e)
- {
- PrikaziSveOsobe();
- }
- private void btnSpremi_Click(object sender, EventArgs e)
- {
- SpremiOsobu();
- }
- private void btnBrisi_Click(object sender, EventArgs e)
- {
- BrisiOsobu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement