.Net ramblings
# Wednesday, 30 August 2006
A javascript function to enable MaxLength behaviour for Multiline Textboxes
http://www.dynamicdrive.com/dynamicindex16/maxlength.htm

<script type="text/javascript">

/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

</script>


<textarea maxlength="40" onkeyup="return ismaxlength(this)"></textarea>


Wednesday, 30 August 2006 17:12:43 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  Asp.Net

# Wednesday, 16 August 2006
Global country list
i took this off Wikipedia and trimmed it down to plain text for a database.  just posting it here for reference:

You may be more interested in the ISO official list of countries and their 2-digit codes.

Abkhazia
Afghanistan
SBA Akrotiri and Dhekelia
Åland
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antigua and Barbuda
Argentina
Armenia
Aruba
Saint Helena Ascension Island
Australia
Austria
Azerbaijan
The Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belgium
Belize
Benin
Bermuda
Bhutan
Bolivia
Bosnia and Herzegovina
Botswana
Brazil
Brunei
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Canada
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Islands
Colombia
Comoros
Congo
Cook Islands
Costa Rica
Côte d'Ivoire
Croatia
Cuba
Cyprus
Czech Republic
Denmark
Djibouti
Dominica
Dominican
Ecuador
Egypt
El Salvador
Equatorial
Eritrea
Estonia
Ethiopia
Falkland
Faroe Islands
Fiji
Finland
France
French Polynesia
Gabon
Gambia
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Honduras
Hong Kong
Hungary
Iceland
India
Indonesia
Iran
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Japan
Jersey
Jordan
Kazakhstan
Kenya
Kiribati
North Korea
South Korea
Kosovo
Kuwait
Kyrgyzstan
Laos
Latvia
Lebanon
Lesotho
Liberia
Libya
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Mauritania
Mauritius
Mayotte
Mexico
Micronesia
Moldova
Monaco
Mongolia
Montenegro
Montserrat
Morocco
Mozambique
Myanmar
Nagorno-Karabakh
Namibia
Nauru
Nepal
Netherlands
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
Northern Cyprus
Northern Mariana Islands
Norway
Oman
Pakistan
Palau
Palestinian
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn Islands
Poland
Portugal
Pridnestrovian Moldavian Republic
Puerto Rico
Qatar
Romania
Russia
Rwanda
Saint Helena
Saint Kitts and Nevis
Saint Lucia
Saint Pierre and Miquelon
Saint Vincent and the Grenadines
Samoa
San Marino
São Tomé and Príncipe
Saudi Arabia
Senegal
Serbia
Seychelles
Sierra Leone
Singapore
Slovakia
Slovenia
Solomon Islands
Somalia
Somaliland
South Africa
South Ossetia
Spain
Sri Lanka
Sudan
Suriname
Svalbard
Swaziland
Sweden
Switzerland
Syria
Tajikistan
Tanzania
Thailand
East Timor
Togo
Tokelau
Tonga
Trinidad and Tobago
Tristan da Cunha
Tunisia
Turkey
Turkmenistan
Turks and Caicos Islands
Tuvalu
Uganda
Ukraine
United Arab Emirates
United Kingdom
United States
Uruguay
Uzbekistan
Vanuatu
Vatican City
Venezuela
Vietnam
Virgin Islands (British)
Virgin Islands (United States)
Wallis and Futuna
Western Sahara
Yemen
Zambia
Zimbabwe


Wednesday, 16 August 2006 16:39:04 (GMT Daylight Time, UTC+01:00)  #    Comments [5]  .Net General | Asp.Net | General

# Friday, 11 August 2006
Crystal Reports: Export PDF irregularities between server and dev machine
I have a few reports in my web application that i export in PDF format only.  You would think this would remove any printer driver complications but apparently not.
Crystal Reports have a surprisingly good article to help troubleshoot issues with printing or exporting a report.

The problem i was finding was that in a Text object or Unbound field, the text would be truncated at the edge of the box boundary.  Some users have reported strange mid-word wrapping, but mine was a different problem, actually hacking off the end of each line.  This didn't happen on my dev machine, but it did happen on the server (2003).  When i compared them side by side, the Times New Roman font on the server was stretched by about 5% compared to my dev machine.
with the help of the CR article, i tracked it down to the lack of any printer installed on the server, i had also disabled the Print Spooler service which was preventing even the Microsoft Office Document Imaging Printer from being available.  So i started the print spooler service and made sure that the Office Imaging Printer was marked as the default printer.  Then i went back to Visual Studio and set all the reports to use the Office Imaging printer.  This solved the problem. 

You could of course install a PDF only printer such as PrimoPDF if you didn't have MS office installed on the server. As long as the server has the same printer installed as the one set in the report design, everything turns out fine.


Friday, 11 August 2006 13:20:57 (GMT Daylight Time, UTC+01:00)  #    Comments [1]  .Net General | .Net Windows Forms | Asp.Net

# Thursday, 10 August 2006
Crystal Reports: join / concatenate null strings
I was getting blank fields in my crystal report if one of the values in the formula was null.
Thanks to this post i know better than to trust Crystal's crummy concatenation rules.  Here is what Gundula Wangerin had to say:
A string concatenated with null always gives null.
You have to check if the first name is null with
     if not IsNull(firstname) then 
        lastname + " " + firstname
    else
        lastname


Thursday, 10 August 2006 17:31:20 (GMT Daylight Time, UTC+01:00)  #    Comments [3]  .Net General | .Net Windows Forms | Asp.Net

# Friday, 04 August 2006
Crystal Reports: Suppress doesn't work for Count(x) = 0
I picked up a tip on the Internet to display a "No records" message if the report contains no records, rather than leaving the user with an empty screen.  Simply Format the text field and enter a formula next to 'Suppress' with something like
Count({Table.Field}) > 0
This will suppress (hide) the message if there are records in the report.

I tried using a similar approach to hide a text field when there are no records, so it is essentially the same thing in reverse.  You would think a simple formula on the 'suppress' property this would achieve the desired effect:
Count({Table.Field}) = 0

But apparently the value returned by Count can be null if there are no records.  so you have to use:

IsNull(Count({Table.Field})) OR Count({Table.Field}) = 0

This is just another crystal reports annoyance, of which there are many.


Friday, 04 August 2006 11:26:38 (GMT Daylight Time, UTC+01:00)  #    Comments [9]  .Net General | Asp.Net | Windows Server

# Wednesday, 02 August 2006
Crystal Reports: not picking up correct date format
i'm part of the dd/MM/yyyy world, and that often means running into problems when using software developed with MM/dd/yyyy defaults, such as crystal reports. 
i have all the report options set to use the system defaults, which are regionally set to ireland in windows, but that isn't enough, the dates still come out in MM/dd/yyyy format.  i have the date + date/time fields customised within crystal reports to dd/MM/yyyy but that isn't enough either.  with lots of hours googling and no answers that worked, i resorted to searching the registry and found that some of the user accounts were still using US regional settings.  If you look in HKEY_USERS > you see a list of all the account IDs on the computer.  My guess is that crystal reports must use the SYSTEM account or another non-interactive account, and it takes the regional settings from there.  So if you search for sShortDate in the registry, you will find all the appropriate settings and can replace the MM/dd/yyyy values with your preferred format.
i had to reboot to get it to take effect. 


Wednesday, 02 August 2006 20:45:07 (GMT Daylight Time, UTC+01:00)  #    Comments [12]  .Net General | Asp.Net | Windows Server

# Thursday, 27 July 2006
Crystal Reports: problems changing database connection at run time
i ran into a problem with my Crystal Reports.  instead of using the usual Typed Dataset approach, where you use Report.SetDataSource(DataSet), i decided that was too much hastle and it seems faster and simpler just to design the query in the Crystal Reports 'wizard', and then supply parameters and set the record filter appropriately.  what i didn't know was that it was insanely complicated to direct the report to a different database path at runtime (such as most users would have in their deployment environment, whether for Access or SQL server). 

i was using Report.SetDataBaseLogon("admin", "") etc and all sorts of variations but none of them were working.  I kept getting Logon Failed exceptions.  Apparently you need to iterate through each table used in the report and set the source database for each one.  Not only that, but neither of the following lines of code has any effect:
Report.Database.Tables[i].SetDataSource("admin", "", DbPath, "")
Report.Database.Tables[i].LogOnInfo.ConnectionInfo.ServerName = DbPath
apparently you have to create a TableLogOnInfo object and use ApplyLogOnInfo() with that on each table. 
here is what does work:
TableLogOnInfo t = new TableLogOnInfo();
t.ConnectionInfo.ServerName = dbPath;            
for(int i=0; i<rpt.Database.Tables.Count; i++)
    rpt.Database.Tables[i].ApplyLogOnInfo(t);


Thursday, 27 July 2006 16:31:47 (GMT Daylight Time, UTC+01:00)  #    Comments [3]  Asp.Net

# Tuesday, 25 July 2006
Network cable unplugged in XP, after installing Vista
i installed vista on a second partition and when i went back to XP, the LAN reported a network cable unplugged.  of course it wasn't, and apparently the IPv6 protocols used in Vista caused some confusion between the router and network card.  turning off the computer and router for a minute or two seems to do the trick!

this useful post explains it better



Tuesday, 25 July 2006 11:05:25 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  

# Monday, 24 April 2006
HowTo: cancel an onbeforeunload event
if you're like me and use the onbeforeunload event in a web page to display a reminder to a user who might unintentially lose work by browsing away from the page, then you might be trying to find a way to cancel the event in the case where the user clicks the save button (they obviously don't need reminding now that they have clicked the button...)

on the internet you will find many people suggesting you use something like
window.onbeforeunload = null
but this does not work for me in IE6.  reading up in the MSDN docs for the event object i found a reference to the event.cancelBubble property, which i thought was the solution. but thanks to Orso who pointed out that setting "event.cancelBubble=true" is useless, the way to get rid of the confirm prompt is to exclude the return statement altogether, i chose to use a boolean variable as a flag to decide whether to return something or not. in the example below i add the javascript code programattically in the code behind:

	Page.ClientScript.RegisterStartupScript(typeof(String), "ConfirmClose", @"
<script>
window.onbeforeunload = confirmExit;
function confirmExit()
{
if(postback == false)
return ""Please don't leave this page without clicking the 'Save Changes' or 'Discard Changes' buttons."";
}
</script>");

then my save button contains the following aspx markup:
    OnClientClick="postback=true;return true;"
this sets the 'postback' variable to true, which gets picked up in the confirmExit() function, having the effect of cancelling the event. 

hope you find this useful.  it is tested and works in IE6 and FF 1.5.0.2.


Monday, 24 April 2006 17:10:13 (GMT Daylight Time, UTC+01:00)  #    Comments [16]  Asp.Net

# Wednesday, 12 April 2006
FindControl() won't find the control....
my aspx page uses a master page, so all the content is inside a ContentPlaceHolder. 
aparently Page.FindControl() only works within a single container, which is really poor functionality if you ask me. 
to complicate the situation further, my aspx page has dynamic controls inside a Panel. 
in order to access the controls of my user control, from the code-behind of the page, i first had to find the panel and then the user control, and then i could finally use that to find the controls i wanted. 
note: you don't have to use the client ID of the control ...ctrl00_blah_blah_blah_YourControlName... when you use the above method, you just use the name of the control, e.g. txtFirstName. 


Wednesday, 12 April 2006 15:27:51 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  Asp.Net