/// <summary>
/// Read an order from the database
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
public OrderInfo GetOrder(int orderId) {
//Create a parameter
SqlParameter parm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
parm.Value = orderId;
//Execute a query to read the order
using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_DTC_ORDERS, CommandType.Text, SQL_SELECT_ORDER, parm)) {
if (rdr.Read()) {
//Generate an order header from the first row
CreditCardInfo creditCard = new CreditCardInfo(rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));
AddressInfo billingAddress = new AddressInfo(rdr.GetString(5), rdr.GetString(6), rdr.GetString(7), rdr.GetString(8), rdr.GetString(9), rdr.GetString(10), rdr.GetString(11), rdr.GetString(12), null);
AddressInfo shippingAddress = new AddressInfo(rdr.GetString(13), rdr.GetString(14), rdr.GetString(15), rdr.GetString(16), rdr.GetString(17), rdr.GetString(18), rdr.GetString(19), rdr.GetString(20), null);
OrderInfo order = new OrderInfo(orderId, rdr.GetDateTime(0), rdr.GetString(1), creditCard, billingAddress, shippingAddress, rdr.GetDecimal(21));
ArrayList lineItems = new ArrayList();
LineItemInfo item = null;
//Create the lineitems from the first row and subsequent rows
do{
item = new LineItemInfo(rdr.GetString(22), string.Empty, rdr.GetInt32(23), rdr.GetInt32(24), rdr.GetDecimal(25));
lineItems.Add(item);
}while(rdr.Read());
/// <summary>
/// Internal function to get cached parameters
/// </summary>
/// <returns></returns>
private static SqlParameter[] GetOrderParameters() {
SqlParameter[] parms = SQLHelper.GetCachedParameters(SQL_INSERT_ORDER);
if (parms == null) {
parms = new SqlParameter[] {
new SqlParameter(PARM_USER_ID, SqlDbType.VarChar, 80),
new SqlParameter(PARM_DATE, SqlDbType.DateTime, 12),
new SqlParameter(PARM_SHIP_ADDRESS1, SqlDbType.VarChar, 80),
new SqlParameter(PARM_SHIP_ADDRESS2, SqlDbType.VarChar, 80),
new SqlParameter(PARM_SHIP_CITY, SqlDbType.VarChar, 80),
new SqlParameter(PARM_SHIP_STATE, SqlDbType.VarChar, 80),
new SqlParameter(PARM_SHIP_ZIP, SqlDbType.VarChar, 50),
new SqlParameter(PARM_SHIP_COUNTRY, SqlDbType.VarChar, 50),
new SqlParameter(PARM_BILL_ADDRESS1, SqlDbType.VarChar, 80),
new SqlParameter(PARM_BILL_ADDRESS2, SqlDbType.VarChar, 80),
new SqlParameter(PARM_BILL_CITY, SqlDbType.VarChar, 80),
new SqlParameter(PARM_BILL_STATE, SqlDbType.VarChar, 80),
new SqlParameter(PARM_BILL_ZIP, SqlDbType.VarChar, 50),
new SqlParameter(PARM_BILL_COUNTRY, SqlDbType.VarChar, 50),
new SqlParameter(PARM_TOTAL, SqlDbType.Decimal, 8),
new SqlParameter(PARM_BILL_FIRST_NAME, SqlDbType.VarChar, 80),
new SqlParameter(PARM_BILL_LAST_NAME, SqlDbType.VarChar, 80),
new SqlParameter(PARM_SHIP_FIRST_NAME, SqlDbType.VarChar, 80),
new SqlParameter(PARM_SHIP_LAST_NAME, SqlDbType.VarChar, 80),
new SqlParameter(PARM_CARD_NUMBER, SqlDbType.VarChar, 80),
new SqlParameter(PARM_CARD_EXPIRATION, SqlDbType.Char, 10),
new SqlParameter(PARM_CARD_TYPE, SqlDbType.VarChar, 80)};
if (parms == null) {
parms = new SqlParameter[] {
//new SqlParameter(PARM_ORDER_ID + i, SqlDbType.Int, 4),
new SqlParameter(PARM_LINE_NUMBER + i, SqlDbType.Int, 4),
new SqlParameter(PARM_ITEM_ID+i, SqlDbType.Char, 10),
new SqlParameter(PARM_QUANTITY+i, SqlDbType.Int, 4),
new SqlParameter(PARM_PRICE+i, SqlDbType.Decimal, 8)};
using System;
using System.Data;
using System.Diagnostics;
using System.Collections;
using System.Data.SqlClient;
using PetShop.Model;
using PetShop.IDAL;
// Create the connection to the database
using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_DTC_ORDERS)) {
// Open the database connection
// Insert the order status
strSQL = SQL_INSERT_ORDER;
SqlParameter[] itemParms ;
// For each line item, insert an orderline record
int i = 0;
foreach (LineItemInfo item in order.LineItems) {
strSQL = strSQL + SQL_INSERT_ITEM + " @ID" + ", @LineNumber"+i + ", @ItemId" + i+ ", @Quantity" + i + ", @Price" + i + "); SELECT @ERR=@ERR+@@ERROR;";
//Get the cached parameters
itemParms = GetItemParameters(i);
// Read the output of the query, should return orderid and error count
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)){
//Read the result
rdr.Read();
// If the error count is not zero throw an exception
if (rdr.GetInt32(1) != 0)
throw new Exception("DATA INTEGRITY ERROR ON ORDER INSERT - ROLLBACK ISSUED");
//Fetch the orderId
orderId = rdr.GetInt32(0);
}
//Clear the parameters
cmd.Parameters.Clear();
}