public ActionResult CheckAddNewOrder(Task11.NewOrderClass newOrder) { int checkID; checkID = _newDal.AddOrder(newOrder); return(View(checkID)); }
public int AddOrder(NewOrderClass NewOrder) { using (var connection = new SqlConnection(ConnectionString)) { int OrderID = 0; var command1 = new SqlCommand( "INSERT INTO Northwind.Orders (Freight,OrderDate,ShippedDate) VALUES (@Freight,@OrderDate,@ShippedDate)", connection); var command2 = new SqlCommand("SELECT OrderID FROM Northwind.Orders WHERE OrderID = (SELECT MAX(OrderID) FROM Northwind.Orders)", connection); command1.Parameters.AddWithValue("@Freight", NewOrder.Freight); if (NewOrder.OrderDate == DateTime.MinValue) { command1.Parameters.AddWithValue("@OrderDate", DBNull.Value); } else { command1.Parameters.AddWithValue("@OrderDate", NewOrder.OrderDate); } if (NewOrder.ShippedDate == DateTime.MinValue) { command1.Parameters.AddWithValue("@ShippedDate", DBNull.Value); } else { command1.Parameters.AddWithValue("@ShippedDate", NewOrder.ShippedDate); } connection.Open(); bool check = (command1.ExecuteNonQuery() == 1); if (check) { using (var reader = command2.ExecuteReader()) { while (reader.Read()) { OrderID = reader.GetInt32(0); } } } return(OrderID); } }
public int AddOrder(NewOrderClass NewOrder) { using (var connection = new SqlConnection(ConnectionString)) { int OrderID = 0; var command1 = new SqlCommand( "INSERT INTO Northwind.Orders (Freight,OrderDate,ShippedDate) VALUES (@Freight,@OrderDate,@ShippedDate)", connection); var command2 = new SqlCommand("SELECT OrderID FROM Northwind.Orders WHERE OrderID = (SELECT MAX(OrderID) FROM Northwind.Orders)", connection); command1.Parameters.AddWithValue("@Freight", NewOrder.Freight); if (NewOrder.OrderDate == DateTime.MinValue) command1.Parameters.AddWithValue("@OrderDate", DBNull.Value); else command1.Parameters.AddWithValue("@OrderDate", NewOrder.OrderDate); if (NewOrder.ShippedDate == DateTime.MinValue) command1.Parameters.AddWithValue("@ShippedDate", DBNull.Value); else command1.Parameters.AddWithValue("@ShippedDate", NewOrder.ShippedDate); connection.Open(); bool check = (command1.ExecuteNonQuery() == 1); if (check) { using (var reader = command2.ExecuteReader()) { while (reader.Read()) { OrderID = reader.GetInt32(0); } } } return OrderID; } }