Tuesday, April 19, 2011

Sort a Listbox using IComparer in C#

What are we cooking today?
Sort a Listbox using IComparer in C# without loosing the value. 

RECIPE

The call
 SortList(MyListbox);

The Procedure

public void SortList(ListBox list)
{
        try
        {
            //---FROM LISTBOX TO LIST<>
            List ListItems = new List();
            foreach (ListItem lst in list.Items)
            {
                ListItems.Add(lst);
            }

            //---SORT LIST<>
            ListItemComparer listC = new ListItemComparer();
            ListItems.Sort(listC.Compare);

            //---CLEAR LISTBOX
            list.Items.Clear();

            //---ADD FROM ordered LIST<> to LISTBOX
            foreach (ListItem li in ListItems)
            {
                list.Items.Add(li);
            }
        }

        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
           
        }//--end catch
    }

The Icomparer: I am using this because a listitem has text and value, and we just want to order by the text, which could be a person's name or a product( the criteria could be also the value like price, personId, year...)


public class ListItemComparer : IComparer
{
    public int Compare(ListItem a, ListItem b)
    {
        int result = string.Compare(a.Text, b.Text);
        return result;
    }
}

Monday, February 28, 2011

Some helpful REGEX (regular expressions for postal code, email, phone, SIN)

What are we cooking today?
Some REGEX (Regular Expresions) for:
  • Canadian/US Postal Code
  • Email (x@x.x)
  • Phone number (xxx)(xxx-xx-xx)
  • Canadian SIN (xxx-xxx-xxx)
Recipe

Valid Canada Postal Code
(^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$)

Valid US and Canada
(\d{5}(-\d{4})?)|(^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$) )

Valid Email
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Valid US Phone
((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}

Canadian SIN
\d{3}-\d{3}-\d{3}

Thursday, February 24, 2011

Tuesday, February 8, 2011

Troubleshooting a web service consuming app error

What are we cooking today?

Troubleshooting :
"Could not find default endpoint element that references contract in the ServiceModel client configuration section"

Recipe
  1. Imagine you want to consume a web service.
  2. You created a class that have the reference to the web service
  3. You created a website (consuming application) that use the class described in (2)
When you run the web app you get this message:
"Could not find default endpoint element that references contract in the ServiceModel client configuration section"

An easy solution that worked for me: Copy all  the content of the MyClassNamespace.dll.config when you compiled your class into the web.config file of the consuming application. (The objective is to expose the endpoint and the correct contract name)

Add to web.config

















Other possible solutions I found when searching:

a) Copy config file (MyApi.dll.config) from class library to bin folder of consuming application and rename it to MyApp.config

b) Programatically create instances of Binding and RemoteAddress objects and use the constructor that takes these 2 parameters

Thursday, December 2, 2010

Clear all the TextBoxes in a form

What are we cooking today?
How to clear TextBoxes in a form that uses masterpages. (C#)


Recipe

This procedure clears all the textboxes in a form. Please note this works when using a masterpage:
  
    public void ClearTextboxesInForm(string strContentPlaceHolder)
    {
        foreach (Control control in Form.Controls) 
        {
            if (control.ID == strContentPlaceHolder) 
            {
                foreach (Control ctrl in control.Controls)
                {
                    if (ctrl is TextBox)  //---Find the textboxes 
                    {
                        ((TextBox)ctrl).Text = "";
                    }
                }
            }
        }
    }

Wednesday, September 22, 2010

Block search engines


What are we cooking today?
How to block Search Engines from indexing site.


Recipe
  1. Add a robot.txt file with instructions to prevent all robots from indexing the page
    User-agent: * Disallow: /
  2. I added a tag in the links to prevent from indexing too.
    Sample http://www.mylink.com rel="nofollow">My Link
  3. I added a metatag :  

    Other details

    To exclude all robots from the entire server
    User-agent: * Disallow: /
    To allow all robots complete access
    User-agent: * Disallow:
    Or create an empty “/robots.txt” file.
    To exclude all robots from part of the server
    User-agent: * Disallow: /cgi-bin/ Disallow: /tmp/ Disallow: /private/
    To exclude a single robot
    User-agent: BadBot Disallow: /
    To allow a single robot
    User-agent: WebCrawler Disallow: User-agent: * Disallow: /
    More on robots --> http://www.robotstxt.org/


Friday, September 10, 2010

MS Access connection on 64 bit

What are we cooking today?
Connecting to an Access Database that was working OK on a 32 bit OS but was moved to a 64bit OS like Windows Server 2008.

If you use Microsoft.Jet.OLEDB.4.0 when your server has 64 bit, it just don't work.

Recipe

Go to 



http://www.microsoft.com/downloads/en/details.aspx?FamilyID=C06B8369-60DD-4B64-A44B-84B371EDE16D&displaylang=en




download and install the Microsoft access database engine .exe and change the connection string that fits your case.


The one that worked for me was:




"...If you are application developer using ODBC to connect to Microsoft Office Access data, set the Connection String to
 “Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file” ..."