Monday, November 14, 2011

ASP.NET Menu Control and Z-INDEX

If you were using the ASP.NET menu control in vs2008, it would (at least for me) always render itself in front of any object. Once I updated to vs2010 and created an ASP.NET menu control, it kept "hiding" itself behind such objects as an embedded pdf. If you look at the source code that vs2008/vs2010 generates it is very different. Vs2008 (.net framework 2.0/3.5) creates tables when rendering a menu control on the client side. Vs2010 (.net framework 4.0) generates DIV/LI/UL statements by default on the client end. If you have tried raising the z-index to a very high level for the menu control in vs2010 and find that it is still rendered behind other objects, the following suggestion might work for you. When you define an ASP.NET menu control add the RenderingMode property and set it to Table:


<asp:Menu ID="mnuMain" runat="server" RenderingMode="Table"...


This will force the menu control to be rendered at the client side using tables and should render the menu control in front of any other control just like in vs2008! Hope this works for you as it worked for me.

Adiel

Wednesday, September 7, 2011

Convert Tab Delimited File to XmlDocument in C#

To convert a tab delimited file to an XmlDocument here is a simple function:


using System.Data;
using System.IO;
using System.Text;
using System.Xml;

...
private XmlDocument getFileData()
{

string path = base.Server.MapPath("tab_data.txt");

DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("MyData");

using (StreamReader sr = new StreamReader(path))
{
int columncount = 0;
string headerRow = sr.ReadLine();
string[] headers = headerRow.Split('\t');
foreach (string h in headers)
{
DataColumn dc = new DataColumn(h.Trim());
dt.Columns.Add(dc);
}
while (sr.Peek() != -1)
{
columncount = 0;
string data = sr.ReadLine();
string[] cells = data.Split('\t');
DataRow row = dt.NewRow();
foreach (string c in cells)
{
row[columncount] = c.Trim();
columncount++;
}
dt.Rows.Add(row);
}
}

// Call Helper function to convert Datatable to XmlDocument
return DataTableToXML(dt);


}

public XmlDocument DataTableToXML(DataTable table)
{

XmlDocument _XMLDoc = new XmlDocument();

_XMLDoc.LoadXml(table.DataSet.GetXml());

return _XMLDoc;

}

Thursday, June 16, 2011

CalendarExtender - Ajax Control Toolkit for ASP.NET

CalendarExtender

If you have ever used the CalendarExtender, you might have received a "Specified cast is not valid" error if populating it with null dates from the database using the SelectedDate property.

-Handling dbnull.

The following SelectedDate property correctly handles dbnulls in the data:

<cc1:CalendarExtender ID="calActivityDate" runat="server"


TargetControlID="txtActivityDate"


SelectedDate='<%# Convert.IsDBNull(Eval("activityDate")) ? null : Eval("activityDate") %>'


PopupButtonID="imgActivityDate" />

Tuesday, October 12, 2010

GCD (Greatest Common Divisor) calculator

There are various ways to calculate the GCD (Greatest Common Divisor). One way was developed by Marcelo Polezzi:


I converted the above into C++:

Header File:

// Gcd.h : Header file for Gcd Class.
//

#pragma once

#include // input/output

// include namespace to type short-form of functions
using namespace std;

class Gcd
{
public:
Gcd(void);
~Gcd(void);
int getFirstInteger();
int getSecondInteger();
void setFirstInteger(int);
void setSecondInteger(int);
int getGcd();
private:
int firstInteger;
int secondInteger;
};

Implementation:
// Gcd.cpp : Gcd Class.
//

#include "StdAfx.h"
#include "Gcd.h"


Gcd::Gcd(void)
{
}

Gcd::~Gcd(void)
{
}

// *****************
// * get functions *
// *****************

int Gcd::getFirstInteger()
{
return firstInteger;
}

int Gcd::getSecondInteger()
{
return secondInteger;
}

// *****************
// * set functions *
// *****************

void Gcd::setFirstInteger(int x)
{
firstInteger = x;
}

void Gcd::setSecondInteger(int x)
{
secondInteger = x;
}

// Creates the GCD
int Gcd::getGcd()
{
// GCD Math Formula
// Mathematician: Marcelo Polezzi

int part1Formula = 0;
int part2Formula = 0;
int firstNumber, secondNumber, gcd;

// Set numbers entered
firstNumber = getFirstInteger();
secondNumber = getSecondInteger();

// Set part 1 of formula
for (int i = 1; i < firstNumber; i++)
{
part1Formula += (i * ((double)secondNumber / (double)firstNumber));
}
part1Formula *= 2;

// Set part 2 of formula
part2Formula = firstNumber + secondNumber - (firstNumber * secondNumber);

// Create GCD
gcd = part1Formula + part2Formula;

return gcd;

}

Instantiating and calling the object:

// This program will display a simple GCD calculator.

#include "stdafx.h"
#include "Gcd.h"

void getTwoIntegers(int &, int &);

int _tmain(int argc, _TCHAR* argv[])
{

// Variables to hold two integers entered by user
int firstInteger, secondInteger;

// Instantiate Gcd class
Gcd myGcd;

// Initial first two integer
getTwoIntegers(firstInteger, secondInteger);
myGcd.setFirstInteger(firstInteger);
myGcd.setSecondInteger(secondInteger);

// Loop calculator
while (firstInteger != -1)
{
// Display two integers
cout << "The GCD of " << firstInteger << " and "
<< secondInteger << " is "
<< myGcd.getGcd() << endl;
// Get next two integers
getTwoIntegers(firstInteger, secondInteger);
myGcd.setFirstInteger(firstInteger);
myGcd.setSecondInteger(secondInteger);
}

system("pause");

return 0;
}

void getTwoIntegers(int &x, int &y)
{

// Get two integers from user
cout << "GCD Calculator (Enter -1 to Quit)" << endl;
cout << "Enter first integer: ";
cin >> x;
if (x != -1)
{
cout << "Enter second integer: ";
cin >> y;
}

}


Adiel

Thursday, February 25, 2010

Resistance Calculator

I have created a small program to calculate Resistance, Power and Current Density. To download the program click here. Here is a sample screen:















Please let me know if you have any suggestions for improving the program.

Adiel

Tuesday, December 1, 2009

Proper Case Function in C#

I found this proper case function but had no way to comment on how to improve it so I will post it in my blog. There are times when you want to capitalize words with hyphens such as capitalizing the S in Ann-Smith. I modified the function as follows:


public static string ProperCase(string stringInput)

{

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    bool fEmptyBefore = true;
    foreach (char ch in stringInput)

    {

        char chThis = ch;
        if (Char.IsWhiteSpace(chThis))
            fEmptyBefore = true;
        else if (Char.IsPunctuation(chThis))
            fEmptyBefore = true; // treat punctuations as spaces
        else
        {
            if (Char.IsLetter(chThis) && fEmptyBefore)
                chThis = Char.ToUpper(chThis);
            else
                chThis = Char.ToLower(chThis);
            fEmptyBefore = false;
        }
        sb.Append(chThis);
    }
    return sb.ToString();

} 


As you can see, the section with the comment "treat punctuations as spaces" was added to handle these situations.

Other Possible Improvements: Handling words such as McDonald.


Adiel

Thursday, November 5, 2009

Data Structures and Algorithms blog

I have created a new blog. The purpose of this new blog is to convert samples from the classic book Data Structures and Algorithms.

Adiel