Advertisement
StevanovicMilan

Vežba 14 - C#

Feb 2nd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11.  
  12. namespace Vezba_14_2018
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
  22.         {
  23.             using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.SportistiConnectionString))
  24.             {
  25.                 connection.Open();
  26.                 string commandText = "SELECT * FROM Sportista";
  27.                 using (SqlCommand command = new SqlCommand(commandText, connection))
  28.                 {
  29.                     using (SqlDataReader reader = command.ExecuteReader())
  30.                     {
  31.                         DataTable dataTable = new DataTable();
  32.                         dataTable.Load(reader);
  33.                         dataGridView1.DataSource = dataTable;
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.  
  39.         private void dodajSportistuToolStripMenuItem_Click(object sender, EventArgs e)
  40.         {
  41.             SportistaForm sForm = new SportistaForm();
  42.             sForm.ShowDialog();
  43.         }
  44.  
  45.         private void obrisiSportistuToolStripMenuItem_Click(object sender, EventArgs e)
  46.         {
  47.             if (dataGridView1.CurrentCell.RowIndex >= 0)
  48.             {
  49.                
  50.                 using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.SportistiConnectionString))
  51.                 {
  52.  
  53.                     connection.Open();
  54.                     string commandText = "DELETE FROM Sportista where Id = {0}";
  55.                     using (SqlCommand command = new SqlCommand(commandText, connection))
  56.                     {
  57.                         using (SqlDataReader reader = command.ExecuteReader())
  58.                         {
  59.                             DataTable dataTable = new DataTable();
  60.                             dataTable.Load(reader);
  61.                             dataGridView1.DataSource = dataTable;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.            
  67.         }
  68.     }
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. using System;
  78. using System.Collections.Generic;
  79. using System.ComponentModel;
  80. using System.Data;
  81. using System.Drawing;
  82. using System.Linq;
  83. using System.Text;
  84. using System.Threading.Tasks;
  85. using System.Windows.Forms;
  86. using System.Data.SqlClient;
  87.  
  88. namespace Vezba_14_2018
  89. {
  90.     public partial class SportistaForm : Form
  91.     {
  92.         public SportistaForm()
  93.         {
  94.             InitializeComponent();
  95.         }
  96.  
  97.         private void button1_Click(object sender, EventArgs e)
  98.         {
  99.             using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.SportistiConnectionString))
  100.             {
  101.                 connection.Open();
  102.                 string commandText = string.Format("INSERT INTO Sportista(Ime,  Visina, Tezina, KlubId, DatumRodjenja) VALUES (N'{0}', {1}, {2}, {3})", textBox1.Text,  numericUpDown1.Value, numericUpDown2.Value, (int)comboBox1.SelectedValue);
  103.                 using (SqlCommand command = new SqlCommand(commandText, connection))
  104.                 {
  105.                     if (command.ExecuteNonQuery() > 0)
  106.                     {
  107.                         MessageBox.Show("Uspesno ste dodali sportistu!");                                            
  108.                     }
  109.                     else
  110.                     {
  111.                         MessageBox.Show("Greska prilikom dodavanja sportiste", "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.  
  117.         private void button2_Click(object sender, EventArgs e)
  118.         {
  119.             Close();
  120.         }
  121.  
  122.         private void SportistaForm_Load(object sender, EventArgs e)
  123.         {
  124.             using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.SportistiConnectionString))
  125.             {
  126.                 connection.Open();
  127.                 string commandText = "SELECT Id, Naziv FROM Klub";
  128.                 using (SqlCommand command = new SqlCommand(commandText, connection))
  129.                 {
  130.                     using (SqlDataReader reader = command.ExecuteReader())
  131.                     {
  132.                         DataTable dataTable = new DataTable();
  133.                         dataTable.Load(reader);
  134.                         comboBox1.DataSource = dataTable;
  135.                         comboBox1.DisplayMember = "Naziv";
  136.                         comboBox1.ValueMember = "Id";
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement