Finalorders.cs

 

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

 

 

namespace prjstart.Components

{

      

 

       public class orderdetails

       {                       

              public DateTime  OrderDate;  

              public DateTime  ShipDate;           // Class defined in order to get the order details on the main webpage

              public decimal   OrderTotal;

              public DataSet   OrderItems;

       }

 

       public class FinalOrders

       {

 

              public orderdetails GetOrderDetails(int orderID, string customerID)

              {

            //  Function to get the order details of the particular customer.Executes the stored procedure listorderdetail.

                    

                     // Requires a orderid and a customerid

                    

                     // Create Instance of Connection and Command Object

                     SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     SqlDataAdapter myCommand = new SqlDataAdapter("listordersdetail", myConnection);

 

                     // Mark the Command as a SPROC

                     myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

 

                     // Add Parameters to SPROC

                     SqlParameter parameterOrderID = new SqlParameter("@OrderID", SqlDbType.Int, 4);

                     parameterOrderID.Value = orderID;

                     myCommand.SelectCommand.Parameters.Add(parameterOrderID);

 

                     SqlParameter parameterCustomerID = new SqlParameter("@CustomerID", SqlDbType.Int, 4);

                     parameterCustomerID.Value = Int32.Parse(customerID);

                     myCommand.SelectCommand.Parameters.Add(parameterCustomerID);

 

                     SqlParameter parameterOrderDate = new SqlParameter("@OrderDate", SqlDbType.DateTime, 8);

                     parameterOrderDate.Direction = ParameterDirection.Output; //  Outputs the order date

                     myCommand.SelectCommand.Parameters.Add(parameterOrderDate);

 

                     SqlParameter parameterShipDate = new SqlParameter("@ShipDate", SqlDbType.DateTime, 8);

                     parameterShipDate.Direction = ParameterDirection.Output;

                     myCommand.SelectCommand.Parameters.Add(parameterShipDate);

 

                     SqlParameter parameterOrderTotal = new SqlParameter("@OrderTotal", SqlDbType.Money, 8);

                     parameterOrderTotal.Direction = ParameterDirection.Output;

                     myCommand.SelectCommand.Parameters.Add(parameterOrderTotal);

 

                     // Create and Fill the DataSet

                     DataSet myDataSet = new DataSet();

                     myCommand.Fill(myDataSet, "OrderItems");

           

                     // ship date is null if order doesn't exist, or belongs to a different user

                     if (parameterShipDate.Value != DBNull.Value)

                     {

           

                           // Create and Populate OrderDetails Struct using

                           // Output Params from the SPROC, as well as the

                           // populated dataset from the SqlDataAdapter

 

                           orderdetails myOrderDetails = new orderdetails();

 

                           myOrderDetails.OrderDate = (DateTime)parameterOrderDate.Value;

                           myOrderDetails.ShipDate = (DateTime)parameterShipDate.Value;

                           myOrderDetails.OrderTotal = (decimal)parameterOrderTotal.Value;

                           myOrderDetails.OrderItems = myDataSet;

 

                           // Return the DataSet

                           return myOrderDetails;

                     }

                     else

                           return null;

              }

 

 

             

              public SqlDataAdapter getCustomerOrders(string custid)

              {

                     SqlConnection objcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     string sql ="Select cout.orderid,cout.customerid,cast(sum(od.quantity * od.unitcost)as money) as totalorders ,cout.orderdate,cout.shipdate from checkoutorders cout,orderdetails od where od.orderid = cout.orderid group by customerid,cout.orderid,cout.orderdate,cout.shipdate having cout.customerid='" + custid +"'";

                     SqlDataAdapter objda = new SqlDataAdapter(sql,objcon);

                     return objda;

              }

 

              public int finalorder(string customerid,string cartid)

              {

                     // Exceutes the stored procedure finalorders on the checkout page ie when the user gets his card validated.

                    

                     SqlConnection objcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     SqlCommand objcom = new SqlCommand("finalOrders",objcon);

 

                     objcom.CommandType = CommandType.StoredProcedure;

                    

                     SqlParameter parametercustomerid = new SqlParameter("@customerid",SqlDbType.Int,4);

                     parametercustomerid.Value = Int32.Parse(customerid);

                     objcom.Parameters.Add(parametercustomerid);

 

                     SqlParameter parametercartid = new SqlParameter("@cartid",SqlDbType.NVarChar,50);

                     parametercartid.Value = cartid;

                     objcom.Parameters.Add(parametercartid);

 

                     SqlParameter parameterorderdate = new SqlParameter("@orderdate",SqlDbType.DateTime,10);

                     parameterorderdate.Value = DateTime.Now;

                     objcom.Parameters.Add(parameterorderdate);

 

                     SqlParameter parametershipdate = new SqlParameter("@shipdate",SqlDbType.DateTime,10);

                     parametershipdate.Value =Calculateshippingdate(customerid,cartid);

                     objcom.Parameters.Add(parametershipdate);

 

                     SqlParameter parameterorderid = new SqlParameter("@orderid",SqlDbType.Int,4);

                     parameterorderid.Direction=ParameterDirection.Output;

                     objcom.Parameters.Add(parameterorderid);

 

                     objcon.Open();

                     objcom.ExecuteNonQuery();

                     objcon.Close();

 

                     return (int)parameterorderid.Value;

              }

 

              public DateTime Calculateshippingdate(string customerid,string cartid)

              {

                     Random x = new Random();

                     double myrandom = (double)x.Next(0,3); // This function calculates the random date based on present day plus three days.

                     return DateTime.Now.AddDays(myrandom);

 

                     }

}

}

 

Back to Main Page