Friday, February 17, 2012

Loading a dropdownlist from an XML file


What are we cooking today?
One Way of loading a dropdownlist from an XML file (sample: reading dates from an XML file)

RECIPE
The XML looks like:

<?xml version="1.0" encoding="utf-8" ?> <dates> <LocationA> <date>February 17, 2012</date> <date>March 29, 2012</date> <date>April 26, 2012</date> </LocationA> <LocationB> <date>March 29, 2012</date> <date>April 26, 2012</date> </LocationB> </dates>


The procedure:

Public Sub LoadDatesFromXMLFile(ByVal strOption As String) Try Dim doc As XDocument = XDocument.Load(MapPath("MyFile.xml")) If strOption = " LocationA " Then For Each element As XElement In doc.Descendants(" LocationA ") For Each XmlNode In element.Descendants("date") ddlAvailableDates.Items.Add(XmlNode.Value) Next Next Else For Each element As XElement In doc.Descendants("LocationB") For Each XmlNode In element.Descendants("date") ddlAvailableDates.Items.Add(XmlNode.Value) Next Next End If Catch ex As Exception lblError.Text = ex.Message End Try End Sub

Friday, January 20, 2012

Sorting a dropdownlist in VB.net


What are we cooking today?
One Way of Sorting a dropdownlist in VB.net

RECIPE

The Procedure


    Private Sub SortDropDown(ByVal ddl As DropDownList)


        '---Get listItems from dropDownList
        Dim ddlList As New ArrayList
        For Each li As ListItem In ddl.Items
            ddlList.Add(li)
        Next

        '---Sort arraylist
        ddlList.Sort(New ListItemComparer)


        '---Copy sorted list back into the dropDownList
        ddl.Items.Clear()
        For Each li As ListItem In ddlList
            ddl.Items.Add(li)
        Next

    End Sub
    

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 : Implements IComparer


    Public Function Compare(ByVal x As Object, _
          ByVal y As Object) As Integer _
          Implements IComparer.Compare
        Dim a As ListItem = x
        Dim b As ListItem = y
        Dim c As New CaseInsensitiveComparer
        Return c.Compare(a.Text, b.Text)
    End Function

End Class

Monday, November 7, 2011

Disable caching (Browser back button problem)


When the user hit the back button a locally cached copy of the previous page is retrieved without notifying the server. As a result the Info is out of sync.

Solution 1 --> Disable caching (do not work for all browsers)
Code to disable caching (to be executed everytime thepage is loaded)

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(Now.AddSeconds(-1));   //--- (-1) means the page already expired
Response.Cache.SetNoStore();  //--tell the page not to cache the page
Response.AppendHeader("Pragma", "no-cache");  //---adds a header to the HTTP response object


Solution 2 --> Use timestamps or random numbers to track pages and detect when a page is current

Monday, October 31, 2011

Copy table into new temporary table and put data back in original table


Sometimes you need to modify a table, like add a new column when there are constrains in place and you may be asked to drop the table before you can modify it. One solution is to store data in a temporary table, make the change and place the data back.

This example works when you have an identity column (autoId)

--- (1)Create new table (TempTable1) and insert into this table using SELECT INSERT
SELECT *
INTO TempTable1
FROM Table1

--- (2)Modify the Table1 --> add columns etc

--- (3)Copy the data from TempTable1 to the original Table1
---    It is important to list all the fields but the autoId (if there is one)

INSERT INTO Table1 (col1,col2,....colN)
SELECT col1,col2,....,colN
FROM TempTable1

Tuesday, October 18, 2011

New Ajax Control Toolkit Features


DATE RANGE was added (Sept. 2011). When Using the Calendar Extender, you can specify startDate and endDate, and the user can pick a date within that range.

TWITTER control
 <asp:Twitter ID="Twitter1" runat="server"  Mode="Profile" ScreenName="MyTwitterName"/>

GRAVATAR
         <ajaxToolkit:Gravatar runat="server"
            Email="myEmail@myEmail.com"
            Size="200"
            Rating="R"
            DefaultImageBehavior="Identicon"
            DefaultImage="PathToMyImage" />

Monday, October 17, 2011

Sample of COALESCE in SQL SERVER


DECLARE @List nvarchar(100)
select @List= COALESCE(@List + ',', '') + colValue  from TableFields where categoryId = 15 and LOWER(name) <> 'subject'
select @List

This will return a concatenated string with all the values (in all rows) for the column colValue and categoryId = 15. (the categoryId could be a parameter  @categoryId)

Friday, October 7, 2011

String to Date in VB.NET

Here is a simple sample to parse from string to date and then to an specific format like yyyyMMdd,
which could be handy for file naming.

 Dim strDate As String = dateExpiration.Text
 Dim parsedDate As Date

 parsedDate = DateTime.Parse(strDate)
 txtMsg.Text = parsedDate.ToString("yyyyMMdd")