Advertisement
Velja_Programer

Fudbalski klub i utakmice (combobox)

Jun 7th, 2020
2,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace B6_Fudbalski_klub_i_utakmice
  13. {
  14.     public partial class Form2 : Form
  15.     {
  16.         SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BEDE3AM\SQLEXPRESS;Initial Catalog=B6;Integrated Security=True");
  17.         public Form2()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void Form2_Load(object sender, EventArgs e)
  23.         {
  24.             // TODO: This line of code loads data into the 'b6DataSet.Grad' table. You can move, or remove it, as needed.
  25.             this.gradTableAdapter.Fill(this.b6DataSet.Grad);
  26.            
  27.             string k = "select top 1 * from Stadion";
  28.             int stadion = 0;
  29.             try
  30.             {
  31.                 con.Open();
  32.                 SqlCommand cmd = new SqlCommand(k, con);
  33.                 SqlDataReader dr = cmd.ExecuteReader();
  34.                 while(dr.Read())
  35.                 {
  36.                     textBox1.Text = dr[0].ToString();
  37.                     textBox2.Text = dr[1].ToString();
  38.                     textBox3.Text = dr[2].ToString();
  39.                     textBox4.Text = dr[3].ToString();
  40.                     textBox5.Text = dr[4].ToString();
  41.                     stadion = Convert.ToInt32(dr[5].ToString());
  42.  
  43.                 }
  44.                 dr.Close();
  45.                // MessageBox.Show(stadion.ToString());
  46.  
  47.  
  48.                  string k2 = "select Grad from Grad where GradID="+stadion;
  49.                      SqlCommand cmd2 = new SqlCommand(k2, con);
  50.                      SqlDataReader dr2 = cmd2.ExecuteReader();
  51.                      if(dr2.Read())
  52.                      {
  53.                     comboBox1.SelectedIndex = stadion - 1;
  54.                        }
  55.                  
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 MessageBox.Show(ex.Message);
  60.                 throw;
  61.             }
  62.             finally
  63.             {
  64.                 if (con.State == ConnectionState.Open) con.Close();
  65.             }
  66.         }
  67.  
  68.         private void button3_Click(object sender, EventArgs e)
  69.         {
  70.             try
  71.             {
  72.                 textBox1.Text = "";
  73.                 textBox2.Text = "";
  74.                 textBox3.Text = "";
  75.                 textBox4.Text = "";
  76.                 textBox5.Text = "";
  77.                 comboBox1.Text = "";
  78.                 textBox1.Focus();
  79.             }
  80.             catch (Exception ex)
  81.             {
  82.                 MessageBox.Show(ex.Message);
  83.                 throw;
  84.             }
  85.         }
  86.  
  87.         private void button1_Click(object sender, EventArgs e)
  88.         {
  89.            
  90.             string k = "";
  91.  
  92.             if (textBox1.Text == "")
  93.                 k = "select top 1 * from Stadion";
  94.             else
  95.                 k = "select top 1 * from Stadion where StadionID=" + (Convert.ToInt32(textBox1.Text)-1);
  96.  
  97.  
  98.             try
  99.             {
  100.                
  101.                 con.Open();
  102.                 SqlCommand cmd = new SqlCommand(k, con);
  103.                 SqlDataReader dr = cmd.ExecuteReader();
  104.                 int grad = 0;
  105.                 while (dr.Read())
  106.                 {
  107.                     textBox1.Text = dr[0].ToString();
  108.                     textBox2.Text = dr[1].ToString();
  109.                     textBox3.Text = dr[2].ToString();
  110.                     textBox4.Text = dr[3].ToString();
  111.                     textBox5.Text = dr[4].ToString();
  112.                     grad = Convert.ToInt32(dr[5].ToString());
  113.  
  114.  
  115.                 }
  116.                 dr.Close();
  117.                 string k2 = "";
  118.  
  119.                
  120.                
  121.                     k2 = "select Grad from Grad where GradID=" + grad;
  122.                     SqlCommand cmd2 = new SqlCommand(k2, con);
  123.                     SqlDataReader dr2 = cmd2.ExecuteReader();
  124.                     if (dr2.Read())
  125.                     {
  126.                     comboBox1.SelectedIndex = grad - 1;
  127.                     }
  128.                
  129.             }
  130.             catch (Exception ex)
  131.             {
  132.                 MessageBox.Show(ex.Message);
  133.                 throw;
  134.             }
  135.             finally
  136.             {
  137.                 if (con.State == ConnectionState.Open) con.Close();
  138.             }
  139.         }
  140.  
  141.         private void button2_Click(object sender, EventArgs e)
  142.         {
  143.             string k = "";
  144.  
  145.             if (textBox1.Text == "")
  146.                 k = "select top 1 * from Stadion  order by StadionID desc";
  147.             else
  148.                 k = "select top 1 * from Stadion where StadionID=" + (Convert.ToInt32(textBox1.Text) + 1);
  149.  
  150.  
  151.             try
  152.             {
  153.  
  154.                 con.Open();
  155.                 SqlCommand cmd = new SqlCommand(k, con);
  156.                 SqlDataReader dr = cmd.ExecuteReader();
  157.                 int grad = 0;
  158.                 while (dr.Read())
  159.                 {
  160.                     textBox1.Text = dr[0].ToString();
  161.                     textBox2.Text = dr[1].ToString();
  162.                     textBox3.Text = dr[2].ToString();
  163.                     textBox4.Text = dr[3].ToString();
  164.                     textBox5.Text = dr[4].ToString();
  165.                     grad = Convert.ToInt32(dr[5].ToString());
  166.  
  167.  
  168.                 }
  169.                 dr.Close();
  170.                 string k2 = "";
  171.  
  172.  
  173.  
  174.                 k2 = "select Grad from Grad where GradID=" + grad;
  175.                 SqlCommand cmd2 = new SqlCommand(k2, con);
  176.                 SqlDataReader dr2 = cmd2.ExecuteReader();
  177.                 if (dr2.Read())
  178.                 {
  179.                     comboBox1.SelectedIndex = grad - 1;
  180.                 }
  181.  
  182.             }
  183.             catch (Exception ex)
  184.             {
  185.                 MessageBox.Show(ex.Message);
  186.                 throw;
  187.             }
  188.             finally
  189.             {
  190.                 if (con.State == ConnectionState.Open) con.Close();
  191.             }
  192.         }
  193.  
  194.         private void button4_Click(object sender, EventArgs e)
  195.         {
  196.             string k = "insert into Stadion values("+textBox1.Text+ ", N'" + textBox2.Text + "', N'" + textBox3.Text + "', " + textBox4.Text + ", " + textBox5.Text + ", "+comboBox1.SelectedValue+")";
  197.             try
  198.             {
  199.                 con.Open();
  200.                 SqlCommand cmd = new SqlCommand(k, con);
  201.                 cmd.ExecuteNonQuery();
  202.                 MessageBox.Show("Uspesno ste uneli stadion");
  203.             }
  204.             catch (Exception ex)
  205.             {
  206.                 MessageBox.Show(ex.Message);
  207.                 //throw;
  208.             }
  209.             finally
  210.             {
  211.                 if (con.State == ConnectionState.Open) con.Close();
  212.             }
  213.         }
  214.  
  215.         private void button5_Click(object sender, EventArgs e)
  216.         {
  217.             this.Close();
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement