Advertisement
coasterka

UpdateOrder.cs

May 25th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.72 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Windows.Forms;
  5.  
  6. namespace SalesDesktop
  7. {
  8.     public partial class UpdateOrder : Form
  9.     {
  10.         private int OrderID;
  11.         public UpdateOrder()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.  
  16.         private void UpdateOrder_Load(object sender, EventArgs e)
  17.         {
  18.             // TODO: This line of code loads data into the 'aSPNETDB_DataSet.Orders' table. You can move, or remove it, as needed.
  19.             this.ordersTableAdapter.Fill(this.aSPNETDB_DataSet.Orders);
  20.             IDOrder orderIDFrm = new IDOrder();
  21.             orderIDFrm.ShowDialog();
  22.             OrderID = orderIDFrm.ReturnedID;            
  23.             if (OrderID != 0)
  24.             {
  25.                 Text = "Order " + OrderID.ToString();
  26.                 SqlConnection connection = CreateConnection();
  27.                 using (connection)
  28.                 {
  29.                     SqlCommand command = new SqlCommand(
  30.                         "SELECT OrderID, UserName, CustEmail, ProdName, ProdPrice, Quantity, ShipperName, PaymentTypeName, OrderDate, DeliveryNotes, OrderAmount FROM Orders WHERE OrderID=@ordID", connection
  31.                         );
  32.                     SqlParameter par = new SqlParameter("@ordID", SqlDbType.Int);
  33.                     par.Value = OrderID;
  34.                     command.Parameters.Add(par);
  35.  
  36.                     try
  37.                     {
  38.                         connection.Open();
  39.                         SqlDataReader reader = command.ExecuteReader();
  40.  
  41.                         while (!reader.HasRows)
  42.                         {
  43.                             MessageBox.Show("There is no product with this ID! Please enter an existing ID!");
  44.                             orderIDFrm.Dispose();
  45.                 //PROBLEM: ObjectDisposedException
  46.                             orderIDFrm.ShowDialog();
  47.                         }
  48.                         if (reader.HasRows)
  49.                         {
  50.                             DataTable dt = new DataTable();
  51.                             dt.Load(reader);
  52.                             ordersDataGridView.DataSource = dt;
  53.                         }
  54.                         reader.Close();
  55.                     }
  56.                     catch (SqlException se)
  57.                     {
  58.                         MessageBox.Show(se.Message.ToString());
  59.                     }
  60.                 }
  61.                 orderIDFrm.Dispose();
  62.             }
  63.             else if (OrderID == 0)
  64.             {
  65.                 //orderIDFrm.Dispose();
  66.                 this.BeginInvoke(new MethodInvoker(orderIDFrm.Dispose));
  67.             }
  68.         }
  69.  
  70.         public static SqlConnection CreateConnection()
  71.         {
  72.             SqlConnection conn = new SqlConnection();
  73.             conn.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=ASPNETDB.MDF;Persist Security Info=True;User ID=sa;Password=1234";
  74.             return conn;
  75.         }
  76.  
  77.         private void UpdateOrder_FormClosing(object sender, FormClosingEventArgs e)
  78.         {
  79.             string exitMessage = "Are you sure you want to close the Update Order form?";
  80.             string exitCaption = "Exit";
  81.             MessageBoxButtons buttons = MessageBoxButtons.YesNo;
  82.             MessageBoxIcon icon = MessageBoxIcon.Question;
  83.             DialogResult result = MessageBox.Show(exitMessage, exitCaption, buttons, icon);
  84.             if (result == DialogResult.No)
  85.             {
  86.                 e.Cancel = true;
  87.             }
  88.         }
  89.  
  90.         private void btnCancelUpdateOrder_Click(object sender, EventArgs e)
  91.         {
  92.             this.Close();
  93.         }
  94.  
  95.         private void btnUpdateOrder_Click(object sender, EventArgs e)
  96.         {
  97.             if (ordersDataGridView.CurrentRow.Cells[0].Value.ToString() != OrderID.ToString())
  98.             {
  99.                 MessageBox.Show("You can only change the order with ID " + OrderID.ToString());
  100.             }
  101.             else
  102.             {
  103.                 SqlConnection conn = CreateConnection();
  104.                 using (conn)
  105.                 {
  106.                     SqlCommand command = new SqlCommand("UPDATE Orders SET UserName=@UserName, CustEmail=@CustEmail, ProdName=@ProdName, ProdPrice=@ProdPrice, Quantity=@Quantity, ShipperName=@ShipperName, PaymentTypeName=@PaymentTypeName, OrderDate=@OrderDate, DeliveryNotes=@DeliveryNotes, OrderAmount=@OrderAmount WHERE OrderID=@OrderID", conn);
  107.  
  108.                     SqlParameter UserName = new SqlParameter("@UserName", SqlDbType.NVarChar);
  109.                     UserName.Value = ordersDataGridView.CurrentRow.Cells[1].Value;
  110.                     command.Parameters.Add(UserName);
  111.  
  112.                     SqlParameter CustEmail = new SqlParameter("@CustEmail", SqlDbType.NVarChar);
  113.                     CustEmail.Value = ordersDataGridView.CurrentRow.Cells[2].Value;
  114.                     command.Parameters.Add(CustEmail);
  115.  
  116.                     SqlParameter ProdName = new SqlParameter("@ProdName", SqlDbType.NVarChar);
  117.                     ProdName.Value = ordersDataGridView.CurrentRow.Cells[3].Value;
  118.                     command.Parameters.Add(ProdName);
  119.  
  120.                     SqlParameter ProdPrice = new SqlParameter("@ProdPrice", SqlDbType.Money);
  121.                     ProdPrice.Value = ordersDataGridView.CurrentRow.Cells[4].Value;
  122.                     command.Parameters.Add(ProdPrice);
  123.  
  124.                     SqlParameter Quantity = new SqlParameter("@Quantity", SqlDbType.Int);
  125.                     Quantity.Value = ordersDataGridView.CurrentRow.Cells[5].Value;
  126.                     command.Parameters.Add(Quantity);
  127.  
  128.                     SqlParameter ShipperName = new SqlParameter("@ShipperName", SqlDbType.NVarChar);
  129.                     ShipperName.Value = ordersDataGridView.CurrentRow.Cells[6].Value;
  130.                     command.Parameters.Add(ShipperName);
  131.  
  132.                     SqlParameter PaymentTypeName = new SqlParameter("@PaymentTypeName", SqlDbType.NVarChar);
  133.                     PaymentTypeName.Value = ordersDataGridView.CurrentRow.Cells[7].Value;
  134.                     command.Parameters.Add(PaymentTypeName);
  135.  
  136.                     SqlParameter OrderDate = new SqlParameter("@OrderDate", SqlDbType.Date);
  137.                     OrderDate.Value = ordersDataGridView.CurrentRow.Cells[8].Value;
  138.                     command.Parameters.Add(OrderDate);
  139.  
  140.                     SqlParameter DeliveryNotes = new SqlParameter("@DeliveryNotes", SqlDbType.NVarChar);
  141.                     DeliveryNotes.Value = ordersDataGridView.CurrentRow.Cells[9].Value;
  142.                     command.Parameters.Add(DeliveryNotes);
  143.  
  144.                     SqlParameter OrderAmount = new SqlParameter("@OrderAmount", SqlDbType.Money);
  145.                     OrderAmount.Value = ordersDataGridView.CurrentRow.Cells[10].Value;
  146.                     command.Parameters.Add(OrderAmount);
  147.  
  148.                     SqlParameter OrderIDSql = new SqlParameter("@OrderID", SqlDbType.Int);
  149.                     OrderIDSql.Value = ordersDataGridView.CurrentRow.Cells[0].Value;
  150.                     command.Parameters.Add(OrderIDSql);
  151.  
  152.                     try
  153.                     {
  154.                         conn.Open();
  155.                         command.ExecuteNonQuery();
  156.                         MessageBox.Show("The order has been successfully updated!");
  157.                     }
  158.                     catch (SqlException se)
  159.                     {
  160.                         MessageBox.Show(se.Message.ToString());
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.  
  166.         private void ordersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
  167.         {
  168.             this.Validate();
  169.             this.ordersBindingSource.EndEdit();
  170.             this.tableAdapterManager.UpdateAll(this.aSPNETDB_DataSet);
  171.  
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement