Thank you for your reply. I have checked the case of the names and they all match, I did noticed the OrderDB's name space was Portal.App_Code and the codebehind for the page I want to use it on was using namespace Purchasing. So I changed OrderDB to use
the namespace Purchasing however this has not helped. I have posted my code for both items and the directories they are in so perhaps you can pinpoint much better than I can what is wrong.
/Purchasing/Checkout.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Portal.Purchasing
{
public partial class Checkout : System.Web.UI.Page
{
private Order order;
private Customer cust;
private ShoppingCart cart;
protected void Page_Load(object sender, EventArgs e)
{
cart = (ShoppingCart)Session["cart"];
if (Session["order"] == null)
{
order = new Order(DateTime.Now,
null, (ShoppingCart)Session["cart"]);
Session["order"] = order;
}
else
{
order = (Order)Session["order"];
cart = order.Cart;
}
cust = new Customer(txtLastName.Text, txtFirstName.Text, txtEmail.Text);
order.Cust = cust;
lblSubtotal.Text = order.SubTotal.ToString("c");
lblShipping.Text = order.Shipping.ToString("c");
lblTotal.Text = order.Total.ToString("c"); cart = (ShoppingCart)Session["cart"];
if (Session["order"] == null)
{
order = new Order(DateTime.Now,
null, (ShoppingCart)Session["cart"]);
Session["order"] = order;
}
else
{
order = (Order)Session["order"];
cart = order.Cart;
}
cust = new Customer(txtLastName.Text, txtFirstName.Text, txtEmail.Text);
order.Cust = cust;
lblSubtotal.Text = order.SubTotal.ToString("c");
lblShipping.Text = order.Shipping.ToString("c");
lblTotal.Text = order.Total.ToString("c");
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
bool success = Portal.Purchasing.OrderDB.WriteOrder(order);
Session["cart"] = null;
Session["order"] = null;
if (success)
Response.Redirect("Completed.aspx");
else
Response.Redirect("Completed.aspx?Error=1");
}
}
}
Yanayaya
Member
35 Points
200 Posts
Re: Bool success
Oct 26, 2011 02:32 PM|LINK
Thank you for your reply. I have checked the case of the names and they all match, I did noticed the OrderDB's name space was Portal.App_Code and the codebehind for the page I want to use it on was using namespace Purchasing. So I changed OrderDB to use the namespace Purchasing however this has not helped. I have posted my code for both items and the directories they are in so perhaps you can pinpoint much better than I can what is wrong.
/Purchasing/Checkout.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Configuration; using System.Collections; using System.Web.Security; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace Portal.Purchasing { public partial class Checkout : System.Web.UI.Page { private Order order; private Customer cust; private ShoppingCart cart; protected void Page_Load(object sender, EventArgs e) { cart = (ShoppingCart)Session["cart"]; if (Session["order"] == null) { order = new Order(DateTime.Now, null, (ShoppingCart)Session["cart"]); Session["order"] = order; } else { order = (Order)Session["order"]; cart = order.Cart; } cust = new Customer(txtLastName.Text, txtFirstName.Text, txtEmail.Text); order.Cust = cust; lblSubtotal.Text = order.SubTotal.ToString("c"); lblShipping.Text = order.Shipping.ToString("c"); lblTotal.Text = order.Total.ToString("c"); cart = (ShoppingCart)Session["cart"]; if (Session["order"] == null) { order = new Order(DateTime.Now, null, (ShoppingCart)Session["cart"]); Session["order"] = order; } else { order = (Order)Session["order"]; cart = order.Cart; } cust = new Customer(txtLastName.Text, txtFirstName.Text, txtEmail.Text); order.Cust = cust; lblSubtotal.Text = order.SubTotal.ToString("c"); lblShipping.Text = order.Shipping.ToString("c"); lblTotal.Text = order.Total.ToString("c"); } protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e) { bool success = Portal.Purchasing.OrderDB.WriteOrder(order); Session["cart"] = null; Session["order"] = null; if (success) Response.Redirect("Completed.aspx"); else Response.Redirect("Completed.aspx?Error=1"); } } }/App_Code/OrderDB.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; namespace Portal.Purchasing { public class OrderDB { static SqTransaction tran; static SqlConnection con; public static bool WriteOrder(Order o) { string cs = WebConfigurationManager .ConnectionStrings["MuirHubProductsConnection"] .ConnectionString; con = new SqlConnection(cs); con.Open(); tran = con.BeginTransaction(); try { InsertCustomer(o.Cust); int oNum = InsertOrder(o); foreach (CartItem item in o.Cart.GetItems()) InsertItem(item, oNum); tran.Commit(); con.Close(); return true; } catch (Exception ex) { tran.RollBack(); return false; } } private static void InsertCustomer(Customer cust) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.Transaction = tran; try { cmd.CommandText = "INSERT INTO Staff " + "(lastname, firstname, email) " + "VALUES (@LastName, @FirstName, @Email)"; cmd.Parameters.AddWithValue( "@LastName", cust.LastName); cmd.Parameters.AddWithValue( "@FirstName", cust.FirstName); cmd.Parameters.AddWithValue( "@Email", cust.Email); cmd.ExecuteNonQuery(); } catch (SqlException ex) { if (ex.Number == 2627) //DuplicateKey { cmd.CommandText = "UPDATE Staff " + "SET lastname = @LastName, " + "firstname = @FirstName " + "WHERE email = @Email "; cmd.ExecuteNonQuery(); } else throw ex; } } private static int InsertOrder(Order o) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.Transaction = tran; cmd.CommandText = "INSERT INTO Orders " + "(orderdate, custemail, " + "subtotal, shipping) " + "VALUE (@OrderDate, @Custemail, " + "@subtotal, @shipping)"; cmd.Parameters.AddWithValue( "@OrderDate", DateTime.Now); cmd.Parameters.AddWithValue( "@Custemail", o.Cust.Email); cmd.Parameters.AddWithValue( "@subtotal", o.SubTotal); cmd.Parameters.AddWithValue( "@shipping", o.Shipping); cmd.ExecuteNonQuery(); cmd.CommandText = "SELECT @@IDENTITY"; return Convert.ToInt32(cmd.ExecuteScalar()); } private static void InsertItem(CartItem item, int oNum) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.Transaction = tran; cmd.CommandText = "INSERT INTO OrderItems " + "(ordernum, productid, " + "name, price, quantity_ " + "VALUES (@OrderNum,, @ProductID, " + "@Name, @Price, @Quantity)"; cmd.Parameters.AddWithValue( "@OrderNum", oNum); cmd.Parameters.AddWithValue( "@ProductID", item.ID); cmd.Parameters.AddWithValue( "@Name", item.Name); cmd.Parameters.AddWithValue( "@Price", item.Price); cmd.Parameters.AddWithValue( "@Quantity", item.Quantity); cmd.ExecuteNonQuery(); } } }