Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if(!Page.IsPostBack)
- {
- IspolniImininja();
- }
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- }
- protected void IspolniImininja()
- {
- lstIme.Items.Clear();
- SqlConnection konekcija = new SqlConnection();
- //konekcija.ConnectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True";
- konekcija.ConnectionString= ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
- SqlCommand komanda = new SqlCommand();
- komanda.Connection = konekcija;
- komanda.CommandText = "SELECT CustomerID, CompanyName FROM Customers ORDER BY CompanyName";
- try
- {
- konekcija.Open();
- SqlDataReader citac = komanda.ExecuteReader();
- while(citac.Read())
- {
- ListItem element = new ListItem();
- element.Text = citac["CompanyName"].ToString();
- element.Value = citac["CustomerID"].ToString();
- lstIme.Items.Add(element);
- }
- citac.Close();
- }
- catch(Exception err)
- {
- lblPoraka.Text = err.Message;
- }
- finally
- {
- konekcija.Close();
- }
- }
- protected void lstIme_SelectedIndexChanged(object sender, EventArgs e)
- {
- selektirajKompanija(lstIme.SelectedItem.Value);
- }
- protected void selektirajKompanija(string id)
- {
- SqlConnection konekcija = new SqlConnection();
- konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
- string sqlString = "SELECT * FROM Customers WHERE CustomerID='" + id + "'";
- SqlCommand komanda = new SqlCommand(sqlString,konekcija);
- try
- {
- konekcija.Open();
- SqlDataReader citac = komanda.ExecuteReader();
- if(citac.Read())
- {
- txtID.Text = citac["CustomerID"].ToString();
- txtIme.Text = citac["CompanyName"].ToString();
- txtKontakt.Text = citac["ContactName"].ToString();
- txtAdresa.Text = citac["Address"].ToString();
- txtGrad.Text = citac["City"].ToString();
- txtTelefon.Text = citac["Phone"].ToString();
- citac.Close();
- }
- }
- catch(Exception err)
- {
- lblPoraka.Text = err.Message;
- }
- finally
- {
- konekcija.Close();
- }
- }
- protected void btnPromeni_Click(object sender, EventArgs e)
- {
- SqlConnection konekcija = new SqlConnection();
- konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
- SqlCommand komanda = new SqlCommand();
- komanda.Connection = konekcija;
- 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+"'";
- try
- {
- konekcija.Open();
- komanda.ExecuteNonQuery();
- }
- catch(Exception err)
- {
- lblPoraka.Text = err.Message;
- }
- finally
- {
- konekcija.Close();
- }
- IspolniImininja();
- }
- protected void btnIscisti_Click(object sender, EventArgs e)
- {
- txtAdresa.Text = "";
- txtGrad.Text = "";
- txtID.Text = "";
- txtIme.Text = "";
- txtKontakt.Text = "";
- txtTelefon.Text = "";
- }
- protected void btnVnesi_Click(object sender, EventArgs e)
- {
- SqlConnection konekcija = new SqlConnection();
- konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
- SqlCommand komanda = new SqlCommand();
- komanda.Connection = konekcija;
- komanda.CommandText = "INSERT INTO Customers(CustomerID, CompanyName, ContactName, Address, City, Phone) VALUES(@CustomerID, @CompanyName, @ContactName, @Address, @City, @Phone)";
- komanda.Parameters.AddWithValue("@CustomerID",txtID.Text);
- komanda.Parameters.AddWithValue("@CompanyName", txtIme.Text);
- komanda.Parameters.AddWithValue("@ContactName", txtKontakt.Text);
- komanda.Parameters.AddWithValue("@Address", txtAdresa.Text);
- komanda.Parameters.AddWithValue("@City", txtGrad.Text);
- komanda.Parameters.AddWithValue("@Phone", txtTelefon.Text);
- try
- {
- konekcija.Open();
- komanda.ExecuteNonQuery();
- }
- catch(Exception err)
- {
- lblPoraka.Text = err.Message;
- }
- finally
- {
- konekcija.Close();
- }
- IspolniImininja();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement