Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9.  
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if(!Page.IsPostBack)
  15.         {
  16.             IspolniImininja();
  17.         }
  18.     }
  19.  
  20.     protected void TextBox1_TextChanged(object sender, EventArgs e)
  21.     {
  22.  
  23.     }
  24.  
  25.     protected void IspolniImininja()
  26.     {
  27.         lstIme.Items.Clear();
  28.  
  29.         SqlConnection konekcija = new SqlConnection();
  30.  
  31.         //konekcija.ConnectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True";
  32.        konekcija.ConnectionString= ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  33.  
  34.         SqlCommand komanda = new SqlCommand();
  35.         komanda.Connection = konekcija;
  36.         komanda.CommandText = "SELECT CustomerID, CompanyName FROM Customers ORDER BY CompanyName";
  37.  
  38.         try
  39.         {
  40.             konekcija.Open();
  41.  
  42.             SqlDataReader citac = komanda.ExecuteReader();
  43.  
  44.             while(citac.Read())
  45.             {
  46.                 ListItem element = new ListItem();
  47.                 element.Text = citac["CompanyName"].ToString();
  48.                 element.Value = citac["CustomerID"].ToString();
  49.  
  50.                 lstIme.Items.Add(element);
  51.             }
  52.             citac.Close();
  53.         }
  54.         catch(Exception err)
  55.         {
  56.             lblPoraka.Text = err.Message;
  57.         }
  58.         finally
  59.         {
  60.             konekcija.Close();
  61.         }
  62.     }
  63.  
  64.     protected void lstIme_SelectedIndexChanged(object sender, EventArgs e)
  65.     {
  66.         selektirajKompanija(lstIme.SelectedItem.Value);
  67.     }
  68.     protected void selektirajKompanija(string id)
  69.     {
  70.         SqlConnection konekcija = new SqlConnection();
  71.         konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  72.         string sqlString = "SELECT * FROM Customers WHERE CustomerID='" + id + "'";
  73.         SqlCommand komanda = new SqlCommand(sqlString,konekcija);
  74.  
  75.         try
  76.         {
  77.             konekcija.Open();
  78.             SqlDataReader citac = komanda.ExecuteReader();
  79.             if(citac.Read())
  80.             {
  81.                 txtID.Text = citac["CustomerID"].ToString();
  82.                 txtIme.Text = citac["CompanyName"].ToString();
  83.                 txtKontakt.Text = citac["ContactName"].ToString();
  84.                 txtAdresa.Text = citac["Address"].ToString();
  85.                 txtGrad.Text = citac["City"].ToString();
  86.                 txtTelefon.Text = citac["Phone"].ToString();
  87.                 citac.Close();
  88.             }
  89.         }
  90.         catch(Exception err)
  91.         {
  92.             lblPoraka.Text = err.Message;
  93.         }
  94.         finally
  95.         {
  96.             konekcija.Close();
  97.         }
  98.     }
  99.  
  100.     protected void btnPromeni_Click(object sender, EventArgs e)
  101.     {
  102.         SqlConnection konekcija = new SqlConnection();
  103.         konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  104.         SqlCommand komanda = new SqlCommand();
  105.         komanda.Connection = konekcija;
  106.         komanda.CommandText = "UPDATE Customers SET CompanyName = '"+txtIme.Text+"',ContactName = '"+txtKontakt.Text+"',Address = '"+txtAdresa.Text+"',City = '"+txtGrad.Text+"',Phone = '"+txtTelefon.Text+"' WHERE CustomerID = '"+lstIme.SelectedItem.Value+"'";
  107.  
  108.         try
  109.         {
  110.             konekcija.Open();
  111.             komanda.ExecuteNonQuery();
  112.         }
  113.         catch(Exception err)
  114.         {
  115.             lblPoraka.Text = err.Message;
  116.         }
  117.         finally
  118.         {
  119.             konekcija.Close();
  120.         }
  121.         IspolniImininja();
  122.     }
  123.  
  124.     protected void btnIscisti_Click(object sender, EventArgs e)
  125.     {
  126.         txtAdresa.Text = "";
  127.         txtGrad.Text = "";
  128.         txtID.Text = "";
  129.         txtIme.Text = "";
  130.         txtKontakt.Text = "";
  131.         txtTelefon.Text = "";
  132.     }
  133.  
  134.  
  135.     protected void btnVnesi_Click(object sender, EventArgs e)
  136.     {
  137.         SqlConnection konekcija = new SqlConnection();
  138.         konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  139.         SqlCommand komanda = new SqlCommand();
  140.         komanda.Connection = konekcija;
  141.         komanda.CommandText = "INSERT INTO Customers(CustomerID, CompanyName, ContactName, Address, City, Phone) VALUES(@CustomerID, @CompanyName, @ContactName, @Address, @City, @Phone)";
  142.         komanda.Parameters.AddWithValue("@CustomerID",txtID.Text);
  143.         komanda.Parameters.AddWithValue("@CompanyName", txtIme.Text);
  144.         komanda.Parameters.AddWithValue("@ContactName", txtKontakt.Text);
  145.         komanda.Parameters.AddWithValue("@Address", txtAdresa.Text);
  146.         komanda.Parameters.AddWithValue("@City", txtGrad.Text);
  147.         komanda.Parameters.AddWithValue("@Phone", txtTelefon.Text);
  148.         try
  149.         {
  150.             konekcija.Open();
  151.             komanda.ExecuteNonQuery();
  152.         }
  153.         catch(Exception err)
  154.         {
  155.             lblPoraka.Text = err.Message;
  156.         }
  157.         finally
  158.         {
  159.             konekcija.Close();
  160.         }
  161.         IspolniImininja();
  162.     }  
  163.        
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement