Here is what I have come up with and yes it could be written a little differently. I am a novice C# programmer, so this code could be simplified. Here is the doc I edited:
wwwroot>checkoutshipping.aspx.cs
I don't know if it matters where you place it, but I will give you where I placed it and I know it works.
Look for this line:
Code:
ErrorMsgLabel.Text = "";
pnlErrorMsg.Visible = false;
CartItem FirstCartItem = (CartItem)cart.CartItems[0];
Address FirstItemShippingAddress = new Address();
FirstItemShippingAddress.LoadByCustomer(ThisCustomer.CustomerID, FirstCartItem.m_ShippingAddressID, AddressTypes.Shipping);
Here it is:
Code:
//
// ********************************************************
string myMsg = "shoppingcart.aspx?errormsg=" + "We are currently unable to ship ammunition to your state.";
int cntr = 0;
bool noShip = false;
if ( ( FirstItemShippingAddress.State == "MO" ) || ( FirstItemShippingAddress.State == "CA" ) || ( FirstItemShippingAddress.State == "MA" ) || ( FirstItemShippingAddress.State == "IL" ) )
{
// Loop through items in the cart
for(int i = 0; i < 1000; i++)
{
try
{
CartItem cartItem = (CartItem)cart.CartItems[i];
using (SqlConnection con = new SqlConnection(DB.GetDBConn()))
{
con.Open();
using (IDataReader rs1 = DB.GetRS("select CategoryID from ProductCategory with (NOLOCK) where ProductID=" + cartItem.m_ProductID.ToString(), con))
{
if (rs1.Read())
{
int firstCat = DB.RSFieldInt(rs1, "CategoryID");
int lastCat = firstCat;
using (SqlConnection myCon = new SqlConnection(DB.GetDBConn()))
{
myCon.Open();
//CategoryID,ParentCategoryID
using (IDataReader rs2 = DB.GetRS("select * from Category with (NOLOCK) where CategoryID=" + firstCat.ToString(), myCon))
{
if (rs2.Read())
{
int parentCat = DB.RSFieldInt(rs2, "ParentCategoryID");
lastCat = DB.RSFieldInt(rs2, "CategoryID");
while ( true )
{
if ( parentCat != 0 )
{
using (SqlConnection myCon2 = new SqlConnection(DB.GetDBConn()))
{
myCon2.Open();
using (IDataReader rs3 = DB.GetRS("select * from Category with (NOLOCK) where CategoryID=" + parentCat.ToString(), myCon2))
{
if ( rs3.Read() )
{
parentCat = DB.RSFieldInt(rs3, "ParentCategoryID");
lastCat = DB.RSFieldInt(rs3, "CategoryID");
}
}
}
}
else
{
if ( lastCat == 138 )
{
noShip = true;
}
break;
}
}
}
}
}
}
}
}
}
catch { break; }
}
if ( noShip )
{
Response.Redirect( myMsg );
}
}
If anyone would like some explaining I would be more than happy. If you would like to know how to customize this code or integrate your database into it just ask.