Advertisement
Latkoski

edit

Sep 12th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.57 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Web.UI.WebControls;
  6.  
  7. public partial class Pregled : System.Web.UI.Page
  8. {
  9.     protected void Page_Load(object sender, EventArgs e)
  10.     {
  11.         if (!IsPostBack)
  12.         {
  13.             ispolniSuppliers();
  14.         }
  15.     }
  16.  
  17.     protected void ispolniSuppliers()
  18.     {
  19.         SqlConnection conn = new SqlConnection();
  20.         conn.ConnectionString = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
  21.         string sqlString = "SELECT * FROM Suppliers";
  22.         SqlCommand command = new SqlCommand(sqlString,conn);
  23.         SqlDataAdapter adapter = new SqlDataAdapter(command);
  24.         DataSet ds = new DataSet();
  25.        
  26.         try
  27.         {
  28.             conn.Open();
  29.             adapter.Fill(ds);
  30.             gvSuppliers.DataSource = ds;
  31.             gvSuppliers.DataBind();
  32.             ViewState["datasetSuppliers"] = ds;
  33.         }
  34.         catch(Exception err)
  35.         {
  36.  
  37.         }
  38.         finally
  39.         {
  40.             conn.Close();
  41.         }
  42.  
  43.  
  44.     }
  45.  
  46.     protected void gvSuppliers_PageIndexChanging(object sender, GridViewPageEventArgs e)
  47.     {
  48.         gvSuppliers.PageIndex = e.NewPageIndex;
  49.         gvSuppliers.SelectedIndex = -1;
  50.         DataSet ds = (DataSet)ViewState["datasetSuppliers"];
  51.         gvSuppliers.DataSource = ds;
  52.         gvSuppliers.DataBind();
  53.         gvProducts.Visible = false;
  54.     }
  55.  
  56.     protected void ispolniProducts()
  57.     {
  58.         SqlConnection conn = new SqlConnection();
  59.         conn.ConnectionString = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
  60.         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";
  61.         SqlCommand command = new SqlCommand(sqlString, conn);
  62.         command.Parameters.AddWithValue("@SupplierID", gvSuppliers.DataKeys[gvSuppliers.SelectedIndex].Value);
  63.         SqlDataAdapter adapter = new SqlDataAdapter(command);
  64.         DataSet ds = new DataSet();
  65.  
  66.         try
  67.         {
  68.             conn.Open();
  69.             adapter.Fill(ds);
  70.             gvProducts.DataSource = ds;
  71.             gvProducts.DataBind();
  72.             gvProducts.Visible = true;
  73.             ViewState["datasetProducts"] = ds;
  74.         }
  75.         catch (Exception err)
  76.         {
  77.  
  78.         }
  79.         finally
  80.         {
  81.             conn.Close();
  82.         }
  83.  
  84.     }
  85.  
  86.  
  87.     protected void gvSuppliers_SelectedIndexChanged(object sender, EventArgs e)
  88.     {
  89.         ispolniProducts();
  90.     }
  91.  
  92.     protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
  93.     {
  94.         SqlConnection conn = new SqlConnection();
  95.         conn.ConnectionString = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
  96.         string sqlString = "UPDATE Products SET ProductName = @ProductName WHERE ProductID = @ProductID";
  97.         SqlCommand command = new SqlCommand(sqlString, conn);
  98.  
  99.         TextBox tb = (TextBox)gvProducts.Rows[e.RowIndex].Cells[1].Controls[0];
  100.         //lError.Text = tb.Text;
  101.         command.Parameters.AddWithValue("@ProductName",tb.Text);
  102.        
  103.  
  104.         //lError.Text += " " + gvProducts.DataKeys[e.RowIndex].Value;
  105.         command.Parameters.AddWithValue("@ProductID", (int)gvProducts.DataKeys[e.RowIndex].Value);
  106.  
  107.         int effect = 0;
  108.  
  109.         try
  110.         {            
  111.             conn.Open();
  112.             effect = command.ExecuteNonQuery();
  113.            
  114.         }
  115.         catch (Exception err)
  116.         {
  117.             //r.Text += err.Message;
  118.         }
  119.         finally
  120.         {
  121.             conn.Close();
  122.             gvProducts.EditIndex = -1;
  123.  
  124.         }
  125.  
  126.  
  127.         //lError.Text += " " + effect.ToString();
  128.  
  129.         if (effect != 0)
  130.         {
  131.             ispolniProducts();
  132.         }
  133.  
  134.  
  135.     }
  136.  
  137.     protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
  138.     {
  139.         DataSet ds = (DataSet)ViewState["datasetProducts"];
  140.         gvProducts.EditIndex = e.NewEditIndex;
  141.         gvProducts.DataSource = ds;
  142.         gvProducts.DataBind();
  143.     }
  144.  
  145.     protected void gvProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  146.     {
  147.         DataSet ds = (DataSet)ViewState["datasetProducts"];
  148.         gvProducts.EditIndex = -1;
  149.         gvProducts.DataSource = ds;
  150.         gvProducts.DataBind();
  151.     }
  152.  
  153.     protected void gvProducts_SelectedIndexChanged(object sender, EventArgs e)
  154.     {
  155.  
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement