An extensive examination of data structures in .NET

11 07 2007

Check out this great article by Scott Mitchell regarding data structures in .NET 2.0.  It’s a six-part series that pretty much covers all that you’ll need to know.

It goes like this:

Part 1: An Introduction to Data Structures
Part 2: The Queue, Stack, and Hashtable
Part 3: Binary Trees and BSTs
Part 4: Building a Better Binary Search Tree
Part 5: From Trees to Graphs
Part 6: Efficiently Representing Sets

Enjoy!  \m/

 



Bind an XMLDataSource to a GridView control

27 06 2007

On one of my ASP.NET 2.0 projects, I had to use an XML file as the data source for a GridView control. The problem was that I had never used this method before. But after doing some research, I found it to be very similar (and straightforward) to binding to a database – with the exception of the need to use XPath.

So…without further adieu, here are the steps and code. Enjoy!

1. Create an XML file that contains your data, like below (taken from Microsoft’s website to save a lot of typing). Save it as books.xml.

<?xml version="1.0"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>
      An in-depth look at creating applications
      with XML.
    </description>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>
      A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.
    </description>
  </book>
</catalog>

2. Add a GridView control to your .aspx page and rename it to gdvBooks.
3. Drag an XmlDataSource control to your form, rename it xdsBooks, and assign your XML file (books.xml) in the control’s DataFile property.
4. Next, change to Source view and input the code below.

<asp:GridView ID="gdvServerVersions" runat="server"         
    AutoGenerateColumns="False" DataKeyNames="id"         
    DataSourceID="xdsServerVersions" EmptyDataText="No data available.">                
    <Columns>
        <asp:BoundField HeaderText="Book ID" DataField="id" 
            SortExpression="id" />
        <asp:TemplateField HeaderText="Author">
            <ItemTemplate>
                <%# XPath("author") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
                <%# XPath("title") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Genre">
            <ItemTemplate>
                <%# XPath("genre") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <%# XPath("price") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Publish Date">
            <ItemTemplate>
                <%# XPath("publish_date") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <%# XPath("description")%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 

5. That’s it! Simply navigate to your newly created page and see the results.


				


How to POP3 in C#

25 04 2007

Here’s an excellent how-to on using POP3 in your C# apps.  I’ve used it successfully.

The Pop3 class derives from the System.Net namespace:
public class Pop3 : System.Net.Sockets.TcpClient
 
And the class consists of the following fields and methods:
public ArrayList List()
public class Pop3Message
public void Connect(string server, string username, string password)
public void Disconnect()
public Pop3Message Retrieve(Pop3Message rhs)
public void Delete(Pop3Message rhs)
private void Write(string message)
private string Response()
 
Finally, below is a code snippet on how to use the Pop3 class:
static void Main(string[] args)
{
    try
    {
        Pop3 obj = new Pop3();
        obj.Connect("mail.xxx.com", "yyy", "zzz");
        ArrayList list = obj.List();
        foreach (Pop3Message msg in list)
        {
            Pop3Message msg2 = obj.Retrieve(msg);
            System.Console.WriteLine("Message {0}: {1}",
                msg2.number, msg2.message);
        }
        obj.Disconnect();
    }
    catch (Pop3Exception e)
    {
        System.Console.WriteLine(e.ToString());
    }
    catch (System.Exception e)
    {
        System.Console.WriteLine(e.ToString());
    }
}

Nifty ey?  So check out the how-to article.  Enjoy!



C# to VB.NET translator available online

21 04 2007

I’m not sure if this will work with the upcoming C# 3.0; however, give it a go if you have the need to convert your C# code to VB.  I personally prefer programming in C# since I’m used to the C-like syntax.

Click here to access the page.

Thanks to Alex Lowe’s AspAlliance ASP.NET website for putting this up for the public to use!