Advertisement
Latkoski

испит ит

Sep 11th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10.  
  11.  
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.  
  17.         GridView2.Visible = false;
  18.         if (!IsPostBack) {
  19.             ispolniGrid();
  20.  
  21.         }
  22.     }
  23.  
  24.     protected void ispolniGrid()
  25.     {
  26.         SqlConnection konekcija = new SqlConnection();
  27.         konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  28.         string SQLString = "Select * FROM Suppliers";
  29.         SqlCommand komanda = new SqlCommand(SQLString, konekcija);
  30.         SqlDataAdapter adapter = new SqlDataAdapter(komanda);
  31.         DataSet ds = new DataSet();
  32.         try {
  33.             konekcija.Open();
  34.             adapter.Fill(ds);
  35.             GridView1.DataSource = ds;
  36.             GridView1.DataBind();
  37.             ViewState["dataset"] = ds;
  38.             GridView2.Visible = false;
  39.         }
  40.  
  41.  
  42.         finally {
  43.             konekcija.Close();
  44.         }
  45.     }
  46.  
  47.     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  48.     {
  49.         GridView1.SelectedIndex = -1;
  50.         GridView1.PageIndex = e.NewPageIndex;
  51.         DataSet ds = (DataSet)ViewState["dataset"];
  52.         GridView1.DataSource = ds;
  53.         GridView1.DataBind();
  54.     }
  55.  
  56.  
  57.     protected void GridView1_PageIndexChanged(object sender, EventArgs e)
  58.     {
  59.  
  60.     }
  61.  
  62.  
  63.     protected void ispolniGrid1() {
  64.         SqlConnection konekcija = new SqlConnection();
  65.         konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  66.         string SqlString = "SELECT Products.ProductID, Suppliers.SupplierID, Products.ProductName, Products.UnitsInStock, Products.UnitPrice FROM Suppliers INNER JOIN Products ON Suppliers.SupplierID = Products.SupplierID where Products.SupplierID = @SupplierID";
  67.         SqlCommand komanda = new SqlCommand(SqlString, konekcija);
  68.         komanda.Parameters.AddWithValue("@SupplierID", GridView1.DataKeys[GridView1.SelectedIndex].Value);
  69.         SqlDataAdapter adapter = new SqlDataAdapter(komanda);
  70.         DataSet ds = new DataSet();
  71.         try
  72.         {
  73.             konekcija.Open();
  74.             adapter.Fill(ds);
  75.             GridView2.Visible = true;
  76.             GridView2.DataSource = ds;
  77.             GridView2.DataBind();
  78.         }
  79.  
  80.         catch (Exception err) { }
  81.  
  82.         finally
  83.         {
  84.             konekcija.Close();
  85.         }
  86.  
  87.  
  88.  
  89.     }
  90.  
  91.  
  92.     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  93.     {
  94.         ispolniGrid1();
  95.     }
  96.  
  97.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  98.     {
  99.         DataSet ds = (DataSet)ViewState["dataset"];
  100.         GridView1.EditIndex = e.NewEditIndex;
  101.         GridView1.DataSource = ds;
  102.         GridView1.DataBind();
  103.     }
  104.  
  105.     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  106.     {
  107.         DataSet ds = (DataSet)ViewState["dataset"];
  108.         GridView1.EditIndex = -1;
  109.         GridView1.DataSource = ds;
  110.         GridView1.DataBind();
  111.     }
  112.  
  113.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  114.     {
  115.         SqlConnection konekcija = new SqlConnection();
  116.         konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  117.         string sqlString = "UPDATE ContactName SET ContactName = @ContactName WHERE CompanyID = @CompanyID";
  118.         SqlCommand komanda = new SqlCommand(sqlString, konekcija);
  119.        
  120.         TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
  121.         komanda.Parameters.AddWithValue("@ContactName", tb.Text);
  122.      
  123.  
  124.         int efekt = 0;
  125.         try
  126.         {
  127.             konekcija.Open();
  128.             efekt = komanda.ExecuteNonQuery();
  129.         }
  130.         finally
  131.         {
  132.             konekcija.Close();
  133.             GridView1.EditIndex = -1;
  134.         }
  135.         if (efekt != 0)
  136.         {
  137.             ispolniGrid();
  138.         }
  139.  
  140.     }
  141.  
  142.     protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
  143.     {
  144.         DataSet ds = (DataSet)ViewState["dataset"];
  145.         GridView2.EditIndex = e.NewEditIndex;
  146.         GridView2.DataSource = ds;
  147.         GridView2.DataBind();
  148.     }
  149.  
  150.  
  151.  
  152.  
  153.     protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
  154.     {
  155.         SqlConnection konekcija = new SqlConnection();
  156.         konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
  157.         string sqlString = "UPDATE Products SET ProductName = @ProductName WHERE ProductID = @ProductID";
  158.         SqlCommand komanda = new SqlCommand(sqlString, konekcija);
  159.         TextBox tb = (TextBox)GridView2.Rows[e.RowIndex].Cells[0].Controls[0];
  160.         komanda.Parameters.AddWithValue("@ProductName", tb.Text);
  161.        
  162.         int count = 0;
  163.         try
  164.         {
  165.             konekcija.Open();
  166.             count = komanda.ExecuteNonQuery();
  167.         }
  168.         catch (Exception err) { }
  169.         finally {
  170.             konekcija.Close();
  171.             GridView2.EditIndex = -1;
  172.         }
  173.         if (count != 0) {
  174.             ispolniGrid1();
  175.         }
  176.  
  177.     }
  178.  
  179.    
  180.  
  181.     protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  182.     {
  183.         DataSet ds = (DataSet)ViewState["dataset"];
  184.         GridView2.EditIndex = -1;
  185.         GridView2.DataSource = ds;
  186.         GridView2.DataBind();
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement