Mar 19 2006

CurrentCulture & CurrentUICulture

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 13:10

There has been always a popular question on the ASP.NET forums about the difference between CurrentCulture and CurrentUICulture. I wanted to distinguish things in this post and make it clear once and for all.

  1. CurrentCulture and CurrentUICulture are both of type CultureInfo which is part of the  System.Globalization.
  2. CurrentCulture and CurrentUICulture are boh properties of the current thread represented as: System.Threading.Thread.CurrentThread.CurrentCulture and System.Threading.Thread.CurrentThread.CurrentUICulture
  3. CurrentCulture is used mainly to format the Dates, Currencies, etc ... For that reason it should be set specific. For instance, you need to set the CurrentCulture as:

    System.Threading.Thread.CurrentThread.CurrentCulture= new CultureInfo("ar-LB");

    We have set current culture to be a specific culture and mainly, Arabic - Lebanese. So make sure always to be specific in specifying the
  4. CurrentUICulture is mainly used for language/text translations. So the ResourceManager is based upon the CurrentUICulture. It doesn't matter whether the culture set here is specific or neutral. For instance, either ar or ar-lb works fine with the CurrentUICulture:

    System.Threading.Thread.CurrentThread.CurrentCulture= new CultureInfo("ar"); // ar-lb

    Both work fine in this case.
  5. ASP.NET controls (Calendar for example) with built-in localization cannot use a neutral culture. When you want to make use of the built-in localization of ASP.NET Controls, make sure to specify a specific culture in the CurrentCulture.

Hope this post helps you clarify the differences between CurrentCulture and CurrentUICulture.


Tags: ,

Mar 18 2006

Top X Records with XMLDataSource

Category: ASP.NET 2.0 - GeneralBil@l @ 20:55

I had a question from a friend of mine at work, he's using the DataList with the XmlDataSource. He wanted to get the Top X records fro the XML file to show them in the DataList, so here is a simple way of doing so I found after doing some search online:


asp:XmlDataSource DataFile=Path to XML File" ID="XmlDataSource1"
server" XPath="rss/channel/item[position() < 11]">

As you can see, we are retrieving the TOP 10 Records from the XML file by using the POSITION() method to get less than 11 records, which is 10 or less records.

The DataList is a simple one:

<asp:datalist id="DataList1" runat="server" datasourceid="XmlDataSource1">
                <a href="<%# XPath("link") %>"><%# XPath("title") %></a>

We are binding the DataList to the XmlDataSource and displaying the links retrieved.

Hope this helps,



Mar 15 2006

MasterPages and Browser Detection

Category: ASP.NET 2.0 - GeneralBil@l @ 13:38

A very nice feature of MasterPages in ASP.NET 2.0 is the ability of the ASPX page to detect the browser and render a specific MasterPage for the specific browser.

How is that done? As follows:

<%@ Page Language="C#" Mozilla:MasterPageFile="MozillaMasterPage.master" ie:MasterPageFile="IEMasterPage.master" ... %>

As you can see, we have added two properties to the page directive, which says, when the browser is of the Mozilla family like "FireFox", make use of the MozillaMasterPage.master file, and when the browser is IE, make use of the IEMasterPage.master file.

To get a list of all the browsers you can handle, go to:

{Drive Letter}:/[WINDOWS,WINNT]/Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers

There you can have the list of all the browsers.

The same trick is applied to Themes. For example you can apply a browser theme as:

Hope that helps,



Mar 15 2006

MVP'd Again

Category:Bil@l @ 08:02

I knew today that I have been MVP'd again as an MVP in ASP/ASP.NET for the second consecutive year.

Thank you for putting your trust in me for the second time,



Mar 10 2006

Server.MapPath Analysis

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 08:36

While working with the HttpContext.Current.Server.MapPath(), I discovered something which is that, the MapPath method starts seeking a path Starting from the current directory you are in and not directly from the root directory.

Hope this hint helps,



Tags: ,

Mar 9 2006

How to use &quot;NOT&quot; in SQL Queries

Category:Bil@l @ 20:58

While wokring with C# or VB.NET, we sometimes implement a switcher-statement. For example, in a certain if statement, we need to flip the state of a certain boolean value as:

bool isValid;
if (State == 2)
    isValid = !isValid;

This way, we will flip the value of isValid from false to true whenever the value of State equals 2.

In one of the stored procedures I was working on, I needed a way to Flip the value of a paramter in a table I have, and this parameter is of type bit, so how can we accomplish this in SQL? The answer is so simple, check the example below:

    IsAutoPayment = ~IsAutoPayment
    CustomerId = @CustomerId

That is very useful when you want to change state of a parameter from one state to another,

So, as a conclusion the (~) is equivalent to (!) or (Not) in C# and VB.NET respectively.

Hope this helps,



Mar 9 2006

HttpHandlers, HttpModules, and the FormsAuthentication

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 16:33

I have been working on a Global Error Logging & Handling solution for a web application that is part of a huge system we have been developing in my company.

I used an HttpModule to get attached to the Application_Error event, where I instantiate my own error handler classes and process the error. The processing is done though sending an email to the admin, saving to datatabase, and optionally saving to a text file. After the error handling is finished, I redirect users to "".

The "", has a weird extension right? I have added this extension to the IIS, so that it will send such requests to my web application. In my web application, I built an HttpHandler, that handles such request or pages with the .er extensions. In the ProcessRequest method of the HttpHandler, I was sending a WebRequest to a template page, that has the text to display when we are faced with an exception in our application. All that was going fine. (I could have also used an HttpHandlerFactory instead).

The problem seemed to be as follows: The whole application is set to be "denied by ?", which means only authenticated users are allowed to visit my web appication pages. The problem with my application was that, in the HttpModule, I was redirecting to, since this file doesn't physically exist and I cannot add a <location> tag for this page since, it is not a .NET page and so the location tag won't have any effect on it, for all the above reasons, my application was not running as expected.

Solution? Well, it was an easy yet effective solution, I created a new folder named Include, it is an empty folder. I add a location tag for this folder to be accessible by all users <allow user="*" />. I made the HttpModule redirect to ~/Include/  and that's it worked perfect.

Hope that helps you,


Tags: ,

Mar 5 2006

Using Method inside a DataGrid or GridView TemplateField

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 09:55

Sometimes there is a need to do some action on a field returned from the database and displayed inside a TemplateField inside the DataGrid or GridView columns.

For example, you might have an email address inside your database and you want to set the HREF of the a tag as a link with the "mailto:" appended at the beginning of the field.

I usually use a small helper method, that takes as input the field coming from the databasee and formats for me the display I want. In the example below, I am displaying the full name of the customer or client, as a link to his/her email address.

GridView Example:

<asp:TemplateField HeaderText="Name">
        <a href='<%# FormatUrl(Eval("email1").ToString())%>'><%# Eval("fname") %>,&nbsp;<%# Eval("lname") %></a>

As you can see, I am calling a method called FormatUrl with an input the email field we're getting from the database.

DataGrid Example:

<asp:TemplateColumn HeaderText="Name">
        <a href='<%# FormatUrl(Eval("email1").ToString())%>'><%# Eval("fname") %>,&nbsp;<%# Eval("lname") %></a>

Now the FormatUrl method is as simple as the following:

public string FormatUrl(string email)
    return "mailto:" + email;


The idea is so simple, we are just calling a heler method, giving it the needed fields to return a formatted string as we want.

Hope this helps,


Tags: ,

Mar 4 2006

AddMessageModule in DotNetNuke

Category: DotNetNukeBil@l @ 21:38

I was developing a Custom Registration Module in DotNetNuke. I followed the same technique used in the default registration module that comes with DotNetNuke, however, I had to do my own cutomizations and using my own tables and objects.

I noticed the heavy use of AddModuleMessage used by this module. This method does nothing but show a message at the top of the module.

I digged a bit in the DotNetNuke code and knew that when the page loads, the DNN creates the default page, and adds a PlaceHolder called MessagePlaceHolder(if not mistaken), to the top of the ContentPane Section of the page above any module added. So when an error happens, DNN finds this PlaceHolder, make it visible and display the error inside it.

My problem was that, the registration form found at was a long form, and sometimes the page is showing the last items in the form, and the user pressed the Register button, if there is an error above, the user will stay in his/her same position without noticing what happened. All what happened was that a problem like a field was not filled is required or a system error happened, like username using is used in th database with another user. The latter message is displayed by DNN in the PlaceHolder and the method placing the error there is the AddModuleMessage. What I wanted is that, when an error happens, I need the user to be able to see the error, how? I created the method below to make the page automatically scroll to the PlaceHolder that is showing the error message after a call to the AddModuleMessage method is done, check the code and the comments below:

// Scroll to top of module
public void Scroll2TopModule()
    // Set page position on top when the AddModuleMessage is functional
    // First I found the place holder containing the error message displayed by AddModuleMessage
        PlaceHolder msgHolder = (PlaceHolder)this.Parent.FindControl("MessagePlaceHolder");

    // If this place holder is visible, it is only visible when the AddModuleMesssage is called
    if (msgHolder.Visible)
            // I down casted the current PortalModuleBase’s page into the CDefault, which I 
            // think it is the default page of the portal
            // then, I used the ScrollToControl method found in the default.aspx page, 
            // this method takes as input a control to scroll to
            // I gave it the parent of the Message Holder, why? The Message holder is usually added 
            // to the Content Page, so I told the method to scroll to the head of the Content Pane

I hope this method helps you. It helped me a lot.



Mar 4 2006

Difference between NULL and DBNull

I have always misunderstood the difference between NULL and DBNULL until recently while I was working on ASP.NET web application, and was talking to a Webservice and I found an exception coming from one of the web methods inside the Webservice which is using the SqlCommand.ExecuteScalar() method.

What happens when the ExecuteScalar() method is used? Usually, the ExecuteScalar() method returns the First Column of the First Row returned by the database stored procedure or SQL query.

Suppose for example you are searching for the CustomerPhone of the customer named "Bilal Haidar", and that customer didn't have any record in the database, so a query such as:

public string Get_Customer_ID(string UserName)
   // Prepare database connection
   SqlConnection conn = new SqlConnection("...");
   // Prepare command to execute
   SqlCommand cmd = new SqlCommand("SELECT CustomerPhone FROM Custoers WHERE CustomerName = 'Bilal Haidar'", conn);

   // Execute the command and store result in an object
   object objResult = cmd.ExecuteScalar();

if (objResult == null) return "Customer not found in our database";
if (objResult == System.DBNull.Value) return "Customer found but his/her phone number is null"; return (string) objResult; }

So now here how it goes. The query will search for the Customer Phone. We are returning the result and storing it in the objResult object.

If the result object is null, then there is no record in the database with the username specified.

If the result value is not null, we need to check if the Cutomer Phone field has a value or not, how do you do so? Using the DBNull.Value would check for you if the field returned is null or not.

So, comparing the ExecuteScalar() to null means, we are checking if there is a record returned, and checking if value of the first column of the first row is null or not, means we need to check against DBNul..Value

Hope you have now a better understanding of the difference between NULL and DBNull


Tags: , , , ,