Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Friday, October 19, 2007

ASP .NET - C# Decimal Comparison

A little more shopping cart hacking. Here's how do to Decimal comparisons in C#:


public static decimal GetShippingCost()
{
decimal sum;
decimal shippingCost;
sum = GetSubtotal() - GetCouponDiscount();
if (sum < 0)
sum = 0;

if ( Decimal.Compare(sum, 26.95m) <= 0 )
shippingCost = 8.95m;
else if ( Decimal.Compare(sum, 35.95m) <= 0 )
shippingCost = 11.95m;
else if ( Decimal.Compare(sum, 45.95m) <= 0 )
shippingCost = 12.95m;
else if ( Decimal .Compare(sum, 55.95m) <= 0 )
shippingCost = 16.95m;
else if ( Decimal.Compare(sum, 74.95m) <= 0 )
shippingCost = 18.95m;
else if ( Decimal.Compare(sum, 99.95m) <= 0 )
shippingCost = 20.95m;
else if ( Decimal.Compare(sum, 119.95m) <= 0 )
shippingCost = 22.95m;
else if (Decimal.Compare(sum, 139.95m) <= 0 )
shippingCost = 24.95m;
else if (Decimal.Compare (sum, 159.95m) <= 0 )
shippingCost = 26.95m;
else if (Decimal.Compare(sum, 199.95m) <= 0 )
shippingCost = 29.95m;
else if ( Decimal.Compare(sum, 249.95m ) <= 0 )
shippingCost = 32.95m;
else if (Decimal.Compare(sum, 299.95m) <= 0 )
shippingCost = 34.95m;
else
shippingCost = 34.95m;

return shippingCost;
}

ASP .NET - Redirecting to HTTPS

On a project where I had to install a third party shopping cart, one issue that came up was to make sure all the pages ran under https (many hard coded links used "http").

I put the following code in the Global.asax file:


protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Request.Url.Port != 443)
{
Response.Redirect ("https://www.yourdomain.com" + Request.Url.AbsolutePath, true);
}
}


This uses


Request.IsSecureConnection


to determine if the call was made over SSL.

So


if (Request.IsSecureConnection)
{
Response.Write("HTTPS");
}
else
{
Response.Write("HTTP");
}



Another way to write this:


if (!Request.IsSecureConnection)
{
string serverName =HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);
string filePath = Request.FilePath;
Response.Redirect("https://" + serverName + filePath);
}

Thursday, October 18, 2007

Classic ASP: Sending e-mail (CDONTS -> CDOSYS)

One of today's jobs was fixing a mail script running on classic ASP that died after being moved to a new server. Attempting to send an email resulted in this error:


Microsoft VBScript runtime error '800a01ad'

ActiveX component can't create object: 'CDONTS.Newmail'

/aspmailer.asp, line 45


CDONTS has been deprecated and replaced with CDOSYS.

I updated aspmailer.asp (a somewhat redundantly named script) to use CDOSYS. After the update, I got this error:


The "SendUsing" configuration value is invalid.


So I still need a bit more code.

This site provided the needed fix:

http://classicasp.aspfaq.com/email/how-do-i-send-e-mail-with-cdo.html


<!--
METADATA
TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
NAME="CDO for Windows 2000 Library"
-->
<%
Set cdoConfig = CreateObject("CDO.Configuration")

With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "<enter_mail.server_here>"
.Update
End With

Set cdoMessage = CreateObject("CDO.Message")

With cdoMessage
Set .Configuration = cdoConfig
.From = "from@me.com"
.To = "to@me.com"
.Subject = "Sample CDO Message"
.TextBody = "This is a test for CDO.message"
.Send
End With

Set cdoMessage = Nothing
Set cdoConfig = Nothing
%>


Replace <enter_mail.server_here> with the name or IP of your SMTP mail server ("localhost" may do the trick).

The other example provided is as follows:


<%
sch = "http://schemas.microsoft.com/cdo/configuration/"

Set cdoConfig = CreateObject("CDO.Configuration")

With cdoConfig.Fields
.Item(sch & "sendusing") = 2 ' cdoSendUsingPort
.Item(sch & "smtpserver") = "<enter_mail.server_here>"
.update
End With

Set cdoMessage = CreateObject("CDO.Message")

With cdoMessage
Set .Configuration = cdoConfig
.From = "from@me.com"
.To = "to@me.com"
.Subject = "Sample CDO Message"
.TextBody = "This is a test for CDO.message"
.Send
End With

Set cdoMessage = Nothing
Set cdoConfig = Nothing
%>

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels