Friday 31 October 2008

Using JavaScript or C# to calculate feet and inches to metres and visa versa

Here are some code snippets for JavaScript and C# that show how to convert from say for example 26' 5" into metres 8.05m. The snippets also show converting metres to foot and inches.;

HTML:
<input id="feet" type="text" onkeyup="calculate 'feet')" /> '
<input id="inches" type="text" onkeyup="calculate('inches')" /> "
<input id="metres" type="text" onkeyup="calculate('metres')" />

JavaScript:
<script type="text/javascript">
function calculate(src) {
switch (src) {
case feetCtrl:
calculateFeetAndInchesToMetres();
break;
case inchesCtrl:
calculateFeetAndInchesToMetres();
break;
case metresCtrl:
calculateMetresToFeetAndInches();
break;
}
}

function calculateFeetAndInchesToMetres() {
var feet = $get("feet").value;
var inches = $get("inches").value;
var totalInches = feet * 12;
totalInches = totalInches + (inches * 1);
var cm = totalInches * 2.54;
var metres = cm / 100;
$get("metres").value = metres.toFixed(2);
}

function calculateMetresToFeetAndInches() {
var metres = $get("metres").value;
var metreInches = metres * 39.370078740157477;
$get("feet").value = Math.floor(metreInches / 12);
$get("inches").value = Math.floor(metreInches % 12);
}
</script>

Same functionality in C#
/// 
/// Gets the feet and inches representation of a metric value
/// 
/// The metres to use
/// The correct display string
public static string GetFeetAndInches(double metres)
{
Hashtable values = GetFeetAndInchesValues(metres);
double feet = (double)values["Feet"];
int inches = (int)values["Inches"];
StringBuilder result = new StringBuilder();
string feetText = feet.ToString();
string[] data = feetText.Split('.');
if (data.Length > 0)
{
feetText = data[0];
}
result.Append(feetText);
result.Append("'");
if (inches > 0)
{
result.Append(" ");
result.Append(inches);
result.Append("\"");
}

return result.ToString();
}

/// 
/// Gets the feet and inches of a metric value
/// 
/// The metres to use
/// An array of feet, inches
public static Hashtable GetFeetAndInchesValues(double metres)
{
Hashtable result = new Hashtable();
double metreInches = metres * MetreToInchesRatio;
double feet = metreInches / 12;
int inches = Convert.ToInt32(metreInches % 12);
result.Add("Feet", feet);
result.Add("Inches", inches);

return result;
}

/// 
/// Gets the metre equivalent of feet and inches combination
/// 
/// The foot value
/// The inches value
/// The metre value
public static double GetMetresFromFeetAndInches(double feet, int inches)
{
// Sourced from: http://sg.answers.yahoo.com/question/index?qid=20070403095853AA3FKJZ
double totalInches = feet * 12;
totalInches += inches;
double cm = totalInches * 2.54;
double metres = cm / 100;

return metres;
}

Wednesday 8 October 2008

Got this error: syntax error. _getAbbrMonthIndex using auto complete extender

All the solutions I searched for suggested there was an error with my JavaScript or code. They were right!

My problem rose because I had a control on a page which had a script manager proxy control and a bunch of autocompleteextender control's, these extender controls used a scriptpath that pointed to a static web method (page method) on the page that retrieved the nessacary data.

The error arose becuase I was adding to my script manager proxy class a reference to the same page method programtically in the page load method. Anyway removing the programatic inclusion to the script manager solved the problem.

Thought this may help others debug any similar scenarios.

Wednesday 1 October 2008

Export to Excel using ASP.NET

I wanted to create a very simple excel export, from an asp.net 2.0 web application, that would take data and display it somewhat like this:



I used the following C# code. Which can be place within a LinkButton, or Button click event handler:

byte[] data = Encoding.UTF8.GetBytes("
Column 1Column 2
Data 1Data 2
Data 3Data 4
"); Response.Clear(); Response.ClearHeaders(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("content-disposition", "filename=test.xls"); Response.BinaryWrite(data); Response.End();


As you can see I've used an HTML table as the source of the data. You can also style this and Excel will display it correctly using any CSS you provide.

The name of the file is also set to "test.xls" using the header content-disposition.