Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- namespace SalesDesktop
- {
- public partial class UpdateOrder : Form
- {
- private int OrderID;
- public UpdateOrder()
- {
- InitializeComponent();
- }
- private void UpdateOrder_Load(object sender, EventArgs e)
- {
- // TODO: This line of code loads data into the 'aSPNETDB_DataSet.Orders' table. You can move, or remove it, as needed.
- this.ordersTableAdapter.Fill(this.aSPNETDB_DataSet.Orders);
- IDOrder orderIDFrm = new IDOrder();
- orderIDFrm.ShowDialog();
- OrderID = orderIDFrm.ReturnedID;
- if (OrderID != 0)
- {
- Text = "Order " + OrderID.ToString();
- SqlConnection connection = CreateConnection();
- using (connection)
- {
- SqlCommand command = new SqlCommand(
- "SELECT OrderID, UserName, CustEmail, ProdName, ProdPrice, Quantity, ShipperName, PaymentTypeName, OrderDate, DeliveryNotes, OrderAmount FROM Orders WHERE OrderID=@ordID", connection
- );
- SqlParameter par = new SqlParameter("@ordID", SqlDbType.Int);
- par.Value = OrderID;
- command.Parameters.Add(par);
- try
- {
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- while (!reader.HasRows)
- {
- MessageBox.Show("There is no product with this ID! Please enter an existing ID!");
- orderIDFrm.Dispose();
- //PROBLEM: ObjectDisposedException
- orderIDFrm.ShowDialog();
- }
- if (reader.HasRows)
- {
- DataTable dt = new DataTable();
- dt.Load(reader);
- ordersDataGridView.DataSource = dt;
- }
- reader.Close();
- }
- catch (SqlException se)
- {
- MessageBox.Show(se.Message.ToString());
- }
- }
- orderIDFrm.Dispose();
- }
- else if (OrderID == 0)
- {
- //orderIDFrm.Dispose();
- this.BeginInvoke(new MethodInvoker(orderIDFrm.Dispose));
- }
- }
- public static SqlConnection CreateConnection()
- {
- SqlConnection conn = new SqlConnection();
- conn.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=ASPNETDB.MDF;Persist Security Info=True;User ID=sa;Password=1234";
- return conn;
- }
- private void UpdateOrder_FormClosing(object sender, FormClosingEventArgs e)
- {
- string exitMessage = "Are you sure you want to close the Update Order form?";
- string exitCaption = "Exit";
- MessageBoxButtons buttons = MessageBoxButtons.YesNo;
- MessageBoxIcon icon = MessageBoxIcon.Question;
- DialogResult result = MessageBox.Show(exitMessage, exitCaption, buttons, icon);
- if (result == DialogResult.No)
- {
- e.Cancel = true;
- }
- }
- private void btnCancelUpdateOrder_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btnUpdateOrder_Click(object sender, EventArgs e)
- {
- if (ordersDataGridView.CurrentRow.Cells[0].Value.ToString() != OrderID.ToString())
- {
- MessageBox.Show("You can only change the order with ID " + OrderID.ToString());
- }
- else
- {
- SqlConnection conn = CreateConnection();
- using (conn)
- {
- 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);
- SqlParameter UserName = new SqlParameter("@UserName", SqlDbType.NVarChar);
- UserName.Value = ordersDataGridView.CurrentRow.Cells[1].Value;
- command.Parameters.Add(UserName);
- SqlParameter CustEmail = new SqlParameter("@CustEmail", SqlDbType.NVarChar);
- CustEmail.Value = ordersDataGridView.CurrentRow.Cells[2].Value;
- command.Parameters.Add(CustEmail);
- SqlParameter ProdName = new SqlParameter("@ProdName", SqlDbType.NVarChar);
- ProdName.Value = ordersDataGridView.CurrentRow.Cells[3].Value;
- command.Parameters.Add(ProdName);
- SqlParameter ProdPrice = new SqlParameter("@ProdPrice", SqlDbType.Money);
- ProdPrice.Value = ordersDataGridView.CurrentRow.Cells[4].Value;
- command.Parameters.Add(ProdPrice);
- SqlParameter Quantity = new SqlParameter("@Quantity", SqlDbType.Int);
- Quantity.Value = ordersDataGridView.CurrentRow.Cells[5].Value;
- command.Parameters.Add(Quantity);
- SqlParameter ShipperName = new SqlParameter("@ShipperName", SqlDbType.NVarChar);
- ShipperName.Value = ordersDataGridView.CurrentRow.Cells[6].Value;
- command.Parameters.Add(ShipperName);
- SqlParameter PaymentTypeName = new SqlParameter("@PaymentTypeName", SqlDbType.NVarChar);
- PaymentTypeName.Value = ordersDataGridView.CurrentRow.Cells[7].Value;
- command.Parameters.Add(PaymentTypeName);
- SqlParameter OrderDate = new SqlParameter("@OrderDate", SqlDbType.Date);
- OrderDate.Value = ordersDataGridView.CurrentRow.Cells[8].Value;
- command.Parameters.Add(OrderDate);
- SqlParameter DeliveryNotes = new SqlParameter("@DeliveryNotes", SqlDbType.NVarChar);
- DeliveryNotes.Value = ordersDataGridView.CurrentRow.Cells[9].Value;
- command.Parameters.Add(DeliveryNotes);
- SqlParameter OrderAmount = new SqlParameter("@OrderAmount", SqlDbType.Money);
- OrderAmount.Value = ordersDataGridView.CurrentRow.Cells[10].Value;
- command.Parameters.Add(OrderAmount);
- SqlParameter OrderIDSql = new SqlParameter("@OrderID", SqlDbType.Int);
- OrderIDSql.Value = ordersDataGridView.CurrentRow.Cells[0].Value;
- command.Parameters.Add(OrderIDSql);
- try
- {
- conn.Open();
- command.ExecuteNonQuery();
- MessageBox.Show("The order has been successfully updated!");
- }
- catch (SqlException se)
- {
- MessageBox.Show(se.Message.ToString());
- }
- }
- }
- }
- private void ordersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
- {
- this.Validate();
- this.ordersBindingSource.EndEdit();
- this.tableAdapterManager.UpdateAll(this.aSPNETDB_DataSet);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement