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;
}
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#:
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:
This uses
to determine if the call was made over SSL.
So
Another way to write this:
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:
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:
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
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:
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
%>
Wednesday, September 26, 2007
JavaScript Style
As I've been doing a lot of Javascript and Ajax these days, I've found these resources valuable:
JavaScript style and the art of anal retention
Dojo style guide
Ajaxian commentary on Dojo style guide
Crockford style guide, part 1
Crockford style guide, part 2
Ajaxian commentary on Crockford style guide
Mozilla style guide
JavaScript style and the art of anal retention
Dojo style guide
Ajaxian commentary on Dojo style guide
Crockford style guide, part 1
Crockford style guide, part 2
Ajaxian commentary on Crockford style guide
Mozilla style guide
Wednesday, September 19, 2007
Ruby on Rails - Legacy MySQL Database
I'm rewriting a company admin tool written in mod_perl (Catalyst) and replacing it with a new one using Ruby on Rails. At the moment, I have to keep the legacy database/schema. Most of the old tables don't use auto-incrementing keys, which means I won't get the Rails Active::Record magic and I need to work around the keys.
Here's an example table:
mysql> describe clients;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| client_abbrv | varchar(5) | NO | PRI | | |
| client_name | varchar(50) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
To get this to work with Rails, I had to make a few small changes.
I made one small change in the view, which was setting the name of the primary key field. I changed "client_abbrv" to "id" in the form:
app/views/client/new.rhtml
For the model, I had set the primary key with
app/models/clients.rb
Luckily, the table was already pluralized. If it had been "client," then I would have had to add this line:
For the controller, I had to change the create method:
app/controllers/client_controller.rb
That replaced the old create method where I had the normal:
When using
Here's what the controller is doing for the create method:
Here's an example table:
mysql> describe clients;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| client_abbrv | varchar(5) | NO | PRI | | |
| client_name | varchar(50) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
To get this to work with Rails, I had to make a few small changes.
I made one small change in the view, which was setting the name of the primary key field. I changed "client_abbrv" to "id" in the form:
app/views/client/new.rhtml
<%= error_messages_for 'client' %>
<fieldset>
<legend>Add a Client
<% form_tag :action => "create" do %>
<p>
<label for="client_client_name">Client Name:
<%= text_field 'client', 'client_name' %>
</p>
<p>
<label for="client_id">Client Abbrv:
<%= text_field 'client', 'id' %>
</p>
<p>
<%= submit_tag 'Add' %>
</p>
<% end %>
</fieldset>
For the model, I had set the primary key with
set_primary_key.app/models/clients.rb
class Client < ActiveRecord::Base
set_primary_key "client_abbrv"
end
Luckily, the table was already pluralized. If it had been "client," then I would have had to add this line:
def self.table_name() "client" end
For the controller, I had to change the create method:
app/controllers/client_controller.rb
def create
@client = Client.new
@client.id = params[:client][:id]
@client.client_name = params[:client][:client_name]
if @client.save
flash[:notice] = "Client was successfully created."
redirect_to :action => 'list'
else
render :action => 'new'
end
end
That replaced the old create method where I had the normal:
@client = Client.new(params[:client])
When using
set_primary_key, you insert data using "id" and typically select data using the actual field name, "client_abbrv" in this case.Here's what the controller is doing for the create method:
dsparling-imbps-computer:~/my/rubydev/uclick-admin dsparlingimbp$ script/console
Loading development environment.
>> client = Client.new
=> #<Client:0x32b5e14 @new_record=true, @attributes={"client_name"=>nil}>
>> client.id = "abc"
=> "abc"
>> client.client_name = "ABC Company"
=> "ABC Company"
>> client.valid?
=> true
>> client.save
=> true
Saturday, September 8, 2007
Javascript Resources
Yahoo! UI Library: YUI Theater
Douglas Crockford — "The JavaScript Programming Language"
Douglas Crockford — "An Inconvenient API: The Theory of the DOM"
Douglas Crockford — "Advanced JavaScript"
Douglas Crockford
Douglas Crockford's Javascript
Code Conventions for the JavaScript Programming Language
John Resig
John Resig
John Resig - JavaScript Talk at Northeastern (video)
Misc
QuirksMode - JavaScript
netscape devedge: JavaScript Central
webdeveloper.com: JavaScript
Web Reference JavaScript
W3Schools JavaScript
Tutorial
DHTML Goodies
JavaScript Toolbox
JavaScript Toolbox
JavaScript Toolbox - Best Practices
comp.lang.javascript FAQ
Tools
JSLint, The JavaScript Verifier
JSMIN, The JavaScript Minifier
dean.edwards.name/packer/
JsUnit
W3C
W3C Document Object Model
Books
JavaScript: The Definitive Guide by David Flanagan
Pro Javascript Techniques by John Resig
JavaScript: The Good Part by Douglas Crockford
Learning jQuery by Karl Swedberg and Jonathan Chaffer
Douglas Crockford — "The JavaScript Programming Language"
Douglas Crockford — "An Inconvenient API: The Theory of the DOM"
Douglas Crockford — "Advanced JavaScript"
Douglas Crockford
Douglas Crockford's Javascript
Code Conventions for the JavaScript Programming Language
John Resig
John Resig
John Resig - JavaScript Talk at Northeastern (video)
Misc
QuirksMode - JavaScript
netscape devedge: JavaScript Central
webdeveloper.com: JavaScript
Web Reference JavaScript
W3Schools JavaScript
Tutorial
DHTML Goodies
JavaScript Toolbox
JavaScript Toolbox
JavaScript Toolbox - Best Practices
comp.lang.javascript FAQ
Tools
JSLint, The JavaScript Verifier
JSMIN, The JavaScript Minifier
dean.edwards.name/packer/
JsUnit
W3C
W3C Document Object Model
Books
JavaScript: The Definitive Guide by David Flanagan
Pro Javascript Techniques by John Resig
JavaScript: The Good Part by Douglas Crockford
Learning jQuery by Karl Swedberg and Jonathan Chaffer
Saturday, September 1, 2007
Upgrading to PHP5 on Mac OS X 10.4
I've been running PHP5 for ages on my Linux box, but my new (for me) MacBook Pro had PHP4...um, time for an upgrade. Well, easy enough...
I downloaded the PHP5 tarball from http://www.entropy.ch/software/macosx/php/ and did the install.
Before the install, I commented out all the php4 lines in my apache config file(
The entropy package installed under
I downloaded the PHP5 tarball from http://www.entropy.ch/software/macosx/php/ and did the install.
Before the install, I commented out all the php4 lines in my apache config file(
/etc/httpd/httpd.conf). The entropy package installed under
/usr/local/php5 and it added a new config file (/private/etc/httpd/users/+entropy-php.conf), which is included by the main apache httpd.conf file...restart apache, that's it!
Subscribe to:
Posts (Atom)
About Me
- Doug Sparling
- Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.
Labels
- .NET (8)
- accordion (1)
- ActiveRecord (1)
- ajax (2)
- APT (1)
- apt-get (1)
- ASP (1)
- ASP.NET .NET (5)
- Audio (1)
- aws (1)
- Bash (1)
- bdd (1)
- C# (1)
- cache_fu (1)
- caching (1)
- DocBook (1)
- DOM (1)
- Eclipse (1)
- Excel (1)
- Firefox (1)
- gem (5)
- Gems (5)
- git (1)
- GridView (2)
- Hibernate (1)
- iBATIS (1)
- Java (9)
- javascript (9)
- javascript css (1)
- jQuery (4)
- jsspec (1)
- mdb2 (2)
- Merb (2)
- mongrel (2)
- mp3 (2)
- Music (2)
- MySQL (2)
- nginx (1)
- openssl (1)
- osx (3)
- pdocast (1)
- pear (2)
- Perl (7)
- php (5)
- plugin (2)
- podcast (1)
- prototype (1)
- REST (1)
- RMagick MacOSX (1)
- rspec (1)
- ruby (14)
- ruby on rails (21)
- RubyGems Merb (1)
- s3 (1)
- screencasts (1)
- scriptaculous (1)
- scriptrunner ruby rails erlang (1)
- Sortable (1)
- subversion (1)
- testing (1)
- testing erlang tsung (1)
- tomcat (1)
- ubuntu (2)
- WebSphere (1)
- will_paginate (1)