Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
# Monday, March 09, 2009

Tooltips are great additions to any interface and in this article I'm going to show you how to attach a tooltip to control in Silverlight 2.

To start, let's assume that we have a button that we want to add a tooltip to:

[XAML]

<UserControl x:Class="KeithRull.Silverlight.CreatingTooltips.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="#FF1E238E">
        <Button Content="Hover over me!" 
                Height="20" 
                Width="100" Margin="30 40" 
                VerticalAlignment="Top" 
                HorizontalAlignment="Left">
                
        </Button>
    </Grid>
</UserControl>

[Rendered UI]

In order or us to attach a tooltip to our button we need to create a reference to the TooltipService.Tooltip inside the our button's XAML declaration. We also need to setup the Tooltip.Content to tell the compiler what to show when the user hover's to our button.

[XAML]

<UserControl x:Class="KeithRull.Silverlight.CreatingTooltips.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="#FF1E238E">
        <Button Content="Hover over me!" 
                Height="20" 
                Width="100" Margin="30 40" 
                VerticalAlignment="Top" 
                HorizontalAlignment="Left">
            <ToolTipService.ToolTip>
                <ToolTip>
                    <ToolTip.Content>
                        <TextBlock TextWrapping="Wrap">
                            Hooray! I'm alive!.
                        </TextBlock>
                    </ToolTip.Content>
                </ToolTip>
            </ToolTipService.ToolTip>
        </Button>
    </Grid>
</UserControl>

[Rendered UI (at runtime)]

The message "Hooray! I'm alive!" appeared when the user hovers over our button. We can also cutomize how our tooltip appears by adding more XAML elements in the Tooltip.Content property.

[XAML]

<UserControl x:Class="KeithRull.Silverlight.CreatingTooltips.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="#FF1E238E">
        <Button Content="Hover over me!" 
                Height="20" 
                Width="100" Margin="30 40" 
                VerticalAlignment="Top" 
                HorizontalAlignment="Left">
            <ToolTipService.ToolTip>
                <ToolTip Background="#FFFFFF" Width="150">
                    <ToolTip.Content>
                        <StackPanel>
                            <Image Source="images/silverlight.png" />
                            <TextBlock HorizontalAlignment="Right">
                                It rocks like a champ!
                            </TextBlock>
                        </StackPanel>    
                    </ToolTip.Content>
                </ToolTip>
            </ToolTipService.ToolTip>
        </Button>
    </Grid>
</UserControl>

[Rendered UI (at runtime)]

Another customization that you might want to do is specifying the HorizontalOffset and the VerticalOffset. This allows you to specify where the tooltip will appear in reference to your target control.

Monday, March 09, 2009 10:49:38 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Silverlight | Tutorial
# Thursday, January 08, 2009

I had some free time during the holidays and saw at the MIX09 website that they have a contest entitled MIX09 10K Challenge where they ask participant to create a web application that is either using Microsoft® Silverlight™ or Windows Presentation Foundation, as a XAML Browser Application running in Partial Trust or as a ClickOnce application in 10 kilobytes or less. I decided to take a stab at it and this is what I've comed up with

SilverCalendar: A Silverlight Pregancy Calendar

You can see the app live here http://2009.visitmix.com/MIXtify/TenKDisplay.aspx?SubmissionID=0051

Dont forget to vote when you're there :P LOL

My primary motivation when I built the app is the idea of having something that is going to be useful and at the same time fun to build. My wife is currently 34 weeks pregnant and it made me think that a pregnancy calendar would be q great application to do since it relates to me and my current day to day life. I love pregnancy calendars because it gives you a good daily insight on the progress of your baby and what you might expected as you go along in your daily life as a soon to be parent. 

The application that i built is using Microsoft Silverlight 2 with a backend WCF web service. You can find the pregnancy calendar web service here. I wasn't able to add any animations to the application because I decided to concentrate in adding functionality to app rather than the eye candy' With that said, I'll be posting the non-10K application this week.

Be sure to comeback on this blog in the coming weeks because I'll be publishing the source code to both project soon together with a 11 part tutorial and screencast walkthrough that i have prepared to show how to to build an application using .NET 3.5, WCF, WPF and Silverlight.

Thursday, January 08, 2009 7:47:35 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | All about Keith | Contest | Family | Fun Stuff | Tutorial | WPF | WCF | Silverlight | Pregnancy
# Wednesday, October 22, 2008

In part 3 of this 5 part series I am going to show you how to make a Master-Detail View in ASP.NET. I you weren't able to see the two previous post you can check them out here:

This post is a continuation of what we started in the two previous post wherein i show you how to consume a webservice ins ASP.NET and present the values returned by the service to the user in a much more meaninful way.

To start off with this post lets look at how the application from our last article look-liked:

As you can see, we have a textbox that accepts a user-input asking for the symbol to lookup in our webservice, a button that triggers that query event and a DetailsView that displays the returned resultset. This works great.

But what if I have a list of symbols that I want the user to see information regarding them? The answer is to show them in a master-detail view so that the user can get a clearer view of the information on each symbol.

To start with this demo lets begin by deleting everything that we have on the page. Yup! we are starting from scratch so that we can get a clearer picture on how we can build a master-detail view screen. Once everything is cleared on the page let's begin by adding a DataList control on our page with a Label control inside its ItemTemplate:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Keith Rull's Consuming Web Services Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DataList   ID="stockDataList" 
                            runat="server">
                <ItemTemplate>
                    <asp:Label  ID="quoteLabel" 
                                runat="server" 
                                Text='<%# Container.DataItem %>'>
                    </asp:Label>
                </ItemTemplate>
                <SeparatorTemplate>
                    <br />
                </SeparatorTemplate>
            </asp:DataList>
        </div>
    </form>
</body>
</html>

The first thing that you would notice on the code above is that we have assigned a value to the Text property of our Label control. This value signifies that we are binding the Container.DataItem value to the Text property. This DataItem will come from the value that we are going to set in the codebehind file for this page which in this case is just going to be a List<string>.

Next, lets go the codebehind file and create the Page_Load event for our page:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //create a list of stock symbols
            List<string> listOfSymbols = new List<string>();

            //add some symbols to our list
            listOfSymbols.Add("MSFT");
            listOfSymbols.Add("GOOG");
            listOfSymbols.Add("CSCO");
            listOfSymbols.Add("MER");
            listOfSymbols.Add("F");

            //bind the list<string> to our datalist
            stockDataList.DataSource = listOfSymbols;
            stockDataList.DataBind();
        }
    }
}

All i did in the code above is create a list of strings, add some values to it and assign and bind that colletion of strings to our DataList. Running the application now would give us this result:

Nothing really impressive yet. Now let's add the cool part wherein we call a web service and show the information for each stock symbol.

To do that we need to add another DataList inside the ItemTemplate of our first DataList. The second DataList will serve as the list that shows the imformation about the specified execution symbol. We also need to add an event that is triggered each time a ListItem is binded to a data. Below is our modified HTML page:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Keith Rull's Consuming Web Services Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DataList   ID="stockDataList" 
                            runat="server" 
                            OnItemDataBound="stockDataList_ItemDataBound">
                <ItemTemplate>
                    <asp:Label  ID="quoteLabel" 
                                runat="server" 
                                Text='<%# Container.DataItem %>'>
                    </asp:Label>
                    <asp:DataList   ID="stockInformationDataList" 
                                    runat="server">
                        <ItemTemplate>
                            <table  cellpadding="1" 
                                    cellspacing="1" 
                                    border="0" 
                                    width="600px">
                                <tr>
                                    <td><b>Last:</b>&nbsp;<%# Eval("Last") %></td>
                                    <td><b>Date:</b>&nbsp;<%# Eval("Date") %></td>
                                    <td><b>Time:</b>&nbsp;<%# Eval("Time") %></td>
                                    <td><b>Change:</b>&nbsp;<%# Eval("Change") %></td>
                                </tr>
                                <tr>
                                    <td><b>Open:</b>&nbsp;<%# Eval("Open") %></td>
                                    <td><b>Low:</b>&nbsp;<%# Eval("Low") %></td>
                                    <td><b>High:</b>&nbsp;<%# Eval("High") %></td>
                                    <td><b>Volume:</b>&nbsp;<%# Eval("Volume") %></td>
                                </tr>
                                <tr>
                                    <td><b>MktCap:</b>&nbsp;<%# Eval("MktCap") %></td>
                                    <td><b>PrvClose:</b>&nbsp;<%# Eval("PreviousClose") %></td>
                                    <td><b>PerChange:</b>&nbsp;<%# Eval("PercentageChange") %></td>
                                    <td><b>AnnRange:</b>&nbsp;<%# Eval("AnnRange") %></td>
                                </tr>
                                <tr>
                                    <td><b>Earns:</b>&nbsp;<%# Eval("Earns") %></td>
                                    <td><b>P-E:</b>&nbsp;<%# Eval("P-E") %></td>
                                    <td>&nbsp</td>
                                    <td>&nbsp</td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>
                </ItemTemplate>
                <SeparatorTemplate>
                    <br />
                </SeparatorTemplate>
            </asp:DataList>
        </div>
    </form>
</body>
</html>

I modified the ItemTemplate of our second DataList () and added an html table to hold the values that the web service returns. This way the view that we are presenting is much more readable.

Next stop is looking at the code behind where all these magic is going to be wired. All the work for the detail view will happen on the stockDataList_ItemDataBound event and no further modification to our Page_Load event is needed. Let me show you the code for that event before I start explaining every bits and pieces of that code:

protected void stockDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
    //check whether the current listitem is an acceptable ListItemType
    if (e.Item.ItemType == ListItemType.Item
        || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //get the binded symbol
        string symbol = e.Item.DataItem.ToString();

        //find the datalist in our itemtemplate
        Control foundControl = e.Item.FindControl("stockInformationDataList");
        //cast the control to a DataList
        DataList stockInformationDataList = (DataList)foundControl;
        //get the stock information for the specified symbol
        DataSet stockInformationDataSet = StockQuoteHelper.GetStockQuoteDataSet(symbol);

        //bind the dataset to our datalist
        stockInformationDataList.DataSource = stockInformationDataSet;
        stockInformationDataList.DataBind();
    }
}

What we are doing on this blog of code is we are checking if the ItemType of the current item being binded-to is either a ListItemType.Item or ListItemType.AlternatingItem. If it passes this criteria we then read the DataItem that was binded to this ListItem which in this case is our executionSymbol. The next step is to find our stockInformationDataList control and assign the stockInformationDataSet to it. The stockInformationDataSet contains the values that is retrieved from our stock web service by passing the exection symbol to our StockQuoteHelper.GetStockQuoteDataSet method.

Running our completed application will result to this screen:

And that's it! We've accomplished a master-detail view in ASP.NET. Pretty easy right? I hope you learned something from this tutorial. Next up we'll update this project and implement some Asynchronous Web Service calls via ASP.NET AJAX and show you some fun ways to present a master-detail view in a much more interesting way.

As always, source code is available for download here: KeithRull.ConsumingWebServices.Part3.zip (6.9 KB)

As a side note, many thanks to John A. Miller from Trofholz Technologies, Inc. for reminding me about this series. I totally forgot about it already after being assigned to a large project the past few months. Thanks John!

Wednesday, October 22, 2008 8:21:07 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | AJAX | ASP.NET | Fun Stuff | Tutorial
# Tuesday, September 23, 2008

In part 1 of this series i showed you how to specify the voice, gender, rate and volume of the our SpeechSynthesizer object. This time i'm going to show you how to use predefined Voices in your machine and utilize it as hints to your SpeechSynthesizer object.

The first thing that I did to our sample project is change the UI since we would not need the Gender and Age combo box in our form. The result is a UI like this:

Next, we need to figure out a way to extract the names of the installed voices in our machine. To this we need to use the SpeechSynthesizer.GetInstalledVoices() method. This method, when invoked returns a readonly collection of TTS(text-to-speech) voices also known as InstalledVoice objects that are readily available in your machine. The InstalledVoice object contains a property called VoiceInfo which represents the voice information about that TTS voice. To begin our project we need to get all the VoiceInfo objects on each and every InstalledVoice. Below is a code snippet showing how we can accomplish this task:

//create a new speechsynthesizer object
static SpeechSynthesizer speechSynth = new SpeechSynthesizer();
/// <summary>
/// a method that returns all the currently installed voice
/// info objects in the machine
/// </summary>
/// <returns>a list of VoiceInfo objects</returns>
private static List<VoiceInfo> GetInstalledVoices()
{
    //get the current cultureinfo
    CultureInfo currentCulture = CultureInfo.CurrentCulture;

    //use linq to select each voiceinfo object from the intalledvoices collection
    var listOfVoiceInfo = from voice
                              in speechSynth.GetInstalledVoices(currentCulture)
                          select voice.VoiceInfo;

    //return the selected voiceinfo objects
    return listOfVoiceInfo.ToList<VoiceInfo>();
}

Next, we need to bind the resulting list to our voiceComboBox.

private void MainForm_Load(object sender, EventArgs e)
{
    BindData();
}

/// <summary>
/// Bind the voices to our combobox
/// </summary>
private void BindData()
{
    //get the installed voices
    List<VoiceInfo> listOfVoices = GetInstalledVoices();
    //bind the list to our combobox
    voicesComboBox.DataSource = listOfVoices;
    voicesComboBox.DisplayMember = "Name";
}

And finally we need to create the logic inside our button click event to initiate our SpeechSynthesizer

private void speakUpButton_Click(object sender, EventArgs e)
{
    //get the values specified in our form
    string messageToSay = wordsTextBox.Text;
    int selectedVolume = volumeTrackBar.Value;
    int selectedVoiceRate = voiceRateTrackBar.Value;

    //get the selected voice info
    VoiceInfo vi = voicesComboBox.SelectedItem as VoiceInfo;
                
    //specify the volume for our SpeechSynthesizer object
    speechSynth.Volume = selectedVolume;
    //specify the rate  for our SpeechSynthesizer object
    speechSynth.Rate = selectedVoiceRate;
    //specify the voice info specifying the voice to use
    speechSynth.SelectVoice(vi.Name);
    //say the message
    speechSynth.SpeakAsync(messageToSay);
}

And we are done ;) Running the application would show as all the installed TTS voice in our machine. One thing to note is that this list is machine dependent and different machines might contain different voices.

Clicking the "Speak!" button should echo our selected voice.

Next time I'll show you how to export the resulting speech to a wave file. HTH

Tuesday, September 23, 2008 8:54:03 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | Fun Stuff | Tutorial
# Thursday, September 18, 2008

One of the cool things that came out with .NET 3.x is the addition of System.Speech library. This library is a collection of classes that enables you to do alot of speech related things like speech recognition and text-to-speech conversion. It's a handful to talk about so i suggest you read up MSDN to learn more about this library[1][2][3].

The app I'm going to show you today is a basic application that shows how to utilize the SpeechSynthesizer class that is located inside the System.Speech.Synthesis namespace.

To start this demo lets add a reference to the System.Speech namespace to our project. You can do this by doing a right-click on References > Add Reference and selecting System.Speech from the list.

Once added we can now beging utilizing this library by adding a using directive pointing to the specific System.Speech namepace that we want to utilize. For this demo we will use System.Speech.Synthesis

Next, we need to create a new SpeechSynthesizer object. SpeechSynthesizer is class that enables you to convert text-to-speech. The class also has several properties and methods that you can use customize the voice information on your speech synthesizer.

Next is the fun part which is making our application say some words. The cool thing about SpeechSynthersizer is that all you need to do to make your application speak is call the SpeechSynthesizer.Speak() method and your done ;)

Run our application and once started you should here the words "Hello, World" spoken by your machine. Pretty cool huh?!

What we did was a simple demonstration on how to make our apps speak with a few lines using SpeechSynthesizer. But what about customizing the voice? Fear not! I created a sample application that will show you how you can customize the synthesizer by specifying the rate, volume, gender and age of the emitted sound. Below is the screenshot showing the UI for the application

And here is the code snippet with comments detailing how to customize our SpeechSynthesizer object.

using System;
using System.Windows.Forms;
using System.Speech.Synthesis;

namespace KeithRull.TalkToMeGoose
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //create a new speechsynthesizer object
        SpeechSynthesizer speechSynth = new SpeechSynthesizer();

        private void speakUpButton_Click(object sender, EventArgs e)
        {
            //get the values specified in our form
            string messageToSay = wordsTextBox.Text;
            int selectedVolume = volumeTrackBar.Value;
            int selectedVoiceRate = voiceRateTrackBar.Value;
            string selectedGender = genderComboBox.SelectedItem.ToString();
            string selectedAge = ageComboBox.SelectedValue.ToString();

            Type voiceGenderType = typeof(VoiceGender);
            Type voiceAgeType = typeof(VoiceAge);

            //convert the selectedGender value to a VoiceGender
            VoiceGender gender = (VoiceGender)Enum.Parse(voiceGenderType, selectedGender);
            //convert the selectedAge value to a VoiceAge
            VoiceAge age = (VoiceAge)Enum.Parse(voiceAgeType, selectedAge);

            //specify the volume for our SpeechSynthesizer object
            speechSynth.Volume = selectedVolume;
            //specify the rate  for our SpeechSynthesizer object
            speechSynth.Rate = selectedVoiceRate;
            //specify the voice info by using hints regarding the gender and age
            speechSynth.SelectVoiceByHints(gender, age);
            //say the message
            speechSynth.SpeakAsync(messageToSay);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            speechSynth.Speak("Hello, World!");
        }

        /// <summary>
        /// Bind the enums to our combobox
        /// </summary>
        private void BindData()
        {
            BindAgeToComboBox();
            BindGenderToComboBox();
        }

        /// <summary>
        /// bind the VoiceGender enum to our combobox
        /// </summary>
        private void BindGenderToComboBox()
        {
            //convert the enumeration to a string array
            Array voiceGenderArray = Enum.GetValues(typeof(VoiceGender));
            //bind the array to the datasource of our combobox
            genderComboBox.DataSource = voiceGenderArray;
        }

        /// <summary>
        /// Bind the VoiceAge enum to our combobox
        /// </summary>
        private void BindAgeToComboBox()
        {
            //convert the enumeration to a string array
            Array voiceAgeArray = Enum.GetValues(typeof(VoiceAge));
            //bind the array to the datasource of our combobox
            ageComboBox.DataSource = voiceAgeArray;
        }
    }
};

As always, you can download the source code for this project here: KeithRull.TalkToMeGoose1.zip (27.46 KB)

I hope I was able to show you how simple it is to add text to speech functionality to your .NET application. Next time I'll show you how to use predefined voices. Till next time ;)

Thursday, September 18, 2008 6:35:57 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | Fun Stuff | Tutorial
# Friday, September 05, 2008

Ever heard of ColorTranslator? It's a really cool class that you can use to convert colors in .NET. One of the things that it can do is that enables you to convert HTML colors to Windows colors and vice versa. One particular scenario wherein you might want to use this class is when you want to use HTML colors on your WinForm.

Below are examples on how to use this calls:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ColorTranslatorTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //convert a system.drawing.color to html
            Color c1 = Color.AntiqueWhite;
            button1.BackColor = c1;
            button1.Text = ColorTranslator.ToHtml(c1);

            //convert an html color to a gdi+ color
            string htmlColor = "#FFAD55";
            Color c2 = ColorTranslator.FromHtml(htmlColor);
            button2.BackColor = c2;
            button2.Text = ColorTranslator.ToHtml(c2);

            //convert a win32 color to gdi+ color
            int colorHexadecimalValue = 0xA267;
            Color c3 = ColorTranslator.FromWin32(colorHexadecimalValue);
            button3.BackColor = c3;
            button3.Text = ColorTranslator.ToWin32(c3).ToString("X");
        }
    }
}

HTH

Friday, September 05, 2008 5:59:10 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | Tutorial
# Tuesday, August 19, 2008

Ever thought of how to capitalize the first letter of every word in a string? Here's how:

[C#]

using System;
using System.Globalization;

namespace KeithRull.CapitalizingLetters
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string textToTransform = "keith, you need to post more blogs!";

            Console.WriteLine("Original text: " + textToTransform);

            //capitalizing the first letter of our text using the current culture
            Console.WriteLine("Capitalize using the current culture: "
                + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(textToTransform));

            //capitalizing the first letter of our text using a defined culture
            CultureInfo newCultureInfo = new CultureInfo("zh-Hans", false);
            TextInfo textInfo = newCultureInfo.TextInfo;
            Console.WriteLine("Capitalize using a specified culture: " + textInfo.ToTitleCase(textToTransform));

            Console.Read();
        }
    }
}

[VB.NET]

Imports System
Imports System.Globalization
 
Namespace KeithRull.CapitalizingLetters
    Friend Class Program
        Shared  Sub Main(ByVal args() As String)
            Dim textToTransform As String =  "keith, you need to post more blogs!" 
 
            Console.WriteLine("Original text: " + textToTransform)
 
            'capitalizing the first letter of our text using the current culture
            Console.WriteLine("Capitalize using the current culture: "
                Dim CultureInfo.CurrentCulture.TextInfo.ToTitleCase(textToTransform)) As +
 
            'capitalizing the first letter of our text using a defined culture
            Dim NewCultureInfo As CultureInfo =  New CultureInfo("zh-Hans",False) 
            Dim textInfo As TextInfo =  NewCultureInfo.TextInfo 
            Console.WriteLine("Capitalize using a specified culture: " + textInfo.ToTitleCase(textToTransform))
 
            Console.Read()
        End Sub
    End Class
End Namespace

HTH

Tuesday, August 19, 2008 6:54:48 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | Tutorial
# Thursday, April 17, 2008

In my previous article I showed you how to add a web service reference in your ASP.NET application, call a webmethod and the display the values returned byt the web service to a Label. This time I'm going to show you how to transform that Xml string into a DataSet.

I suggest that you read the previous article before continuing in reading this post so you'll have a better insight on what we are trying to accomplish on this article.

Let's begin!

To start off this article let's look at how the application that we built for the previous article.

and the HTML code for the UI

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Keith Rull's Consuming Web Services Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <strong>Symbol</strong><br />
            <asp:TextBox ID="symbolTextBox" 
                         runat="server" />
            <asp:Button ID="executeButton" 
                        runat="server" 
                        Text="Execute"
                        OnClick="executeButton_Click" />
            <br />
            <br />
            <strong>Result</strong>
            <br />
            <asp:Label ID="xmlResultLabel" 
                        runat="server" />
        </div>
    </form>
</body>
</html>

and the underlying code for the click event of the "Execute" button

    protected void executeButton_Click(object sender, EventArgs e)
    {
        //get the execution symbol entered in the TextBox
        string executionSymbol = symbolTextBox.Text;

        //get the stock quote information
        string quoteInfo = GetStockQuoteInformation(executionSymbol);

        //HtmlEncode the string to properly render it on the page
        string htmlEncodedResult = System.Web.HttpUtility.HtmlEncode(quoteInfo);
       
        //assign the HtmlEncoded string to our Label control
        xmlResultLabel.Text = htmlEncodedResult;
    }

As you can see the code above does the job of displaying the retuned xml from the webservice. It works. We can understand that XML because we are techie enough but imagine a normal user seeing a xml values on his screen? Yup. Not good. The best way to present data to a user is to show values in tabular form. A lot more pleasing to the eyes and much easier to understand. With that said let's start by replacing the Label control in the form with a DetailsView control. The new aspx page for our form should look like this:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Keith Rull's Consuming Web Services Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <strong>Symbol</strong><br />
            <asp:TextBox ID="symbolTextBox" 
                         runat="server" />
            <asp:Button ID="executeButton" 
                        runat="server" 
                        Text="Execute"
                        OnClick="executeButton_Click" />
            <br />
            <br />
            <strong>Result</strong>&nbsp;<br />
            <asp:DetailsView ID="stockQuoteInfoDetailsView" runat="server" Height="50px" Width="125px">
            </asp:DetailsView>
        </div>
    </form>
</body>
</html>

As you can see in the old code for the Click event we are able to get the XML and display it on a Label. What we need to do now is to read that XML string and assign it to a our DetailsView but there is one problem... The XML string needs to be converted to an object that supports IList or IEnumerable first before it could be loaded to our DetailsView.

To solve this problem I've decided to create this method that accepts an XML string and converts it to a DataSet.

    /// <summary>
    /// A function that takes an XML string and converts it into a DataSet
    /// </summary>
    /// <param name="xmlString">The xml string to tranform into a DataSet</param>
    /// <returns>The DataSet representing the values and schema from our xml string</returns>
    private DataSet XmlString2DataSet(string xmlString)
    {
        //create a new DataSet that will hold our values
        DataSet quoteDataSet = null;

        //check if the xmlString is not blank
        if (String.IsNullOrEmpty(xmlString)) {
            //stop the processing
            return quoteDataSet;
        }

        try{
            //create a StringReader object to read our xml string
            using (StringReader stringReader = new StringReader(xmlString))
            {
                //initialize our DataSet
                quoteDataSet = new DataSet();

                //load the StringReader to our DataSet
                quoteDataSet.ReadXml(stringReader);
            }
        }
        catch{
            //return null
            quoteDataSet = null;
        }

        //return the DataSet containing the stock information
        return quoteDataSet;
    }

Now we can modify our our executeButton_Click event

    protected void executeButton_Click(object sender, EventArgs e)
    {
        //get the execution symbol entered in the TextBox
        string executionSymbol = symbolTextBox.Text;

        //get the stock quote information
        string quoteInfo = GetStockQuoteInformation(executionSymbol);

        //create our quote DataTable
        DataSet quoteDataSet = XmlString2DataSet(quoteInfo);

        //assign the quote information to our DetailsView
        stockQuoteInfoDetailsView.DataSource = quoteDataSet;
        stockQuoteInfoDetailsView.DataBind();
    }

All we did was pass the xml string to the XmlString2DataSet function to retrieve a DataSet containing the stock quote information and then assigning that DataSet object to our DetailsView for display. Below is how the final form looks-like after our modification

Now that is more presentable! I hope I was able to share with you something useful. Next stop, we'll be building Master-Detail pages in ASP.NET ;)

Care for the code? Grab it here: KeithRull.ConsumingWebServices.Part2.zip (5.76 KB)

Thursday, April 17, 2008 6:27:06 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | ASP.NET | Tutorial
# Tuesday, April 01, 2008

Last week I showed you how to read an XML file, load it to a DataSet and assign those values into a GridView. Today I'll show you how you can read an XML file using the XmlDataSource object.

Let's assume that we have an XML file called Symbols.xml in our App_Data folder

that contains the following data

<?xml version="1.0" encoding="utf-8" ?>
<Symbols>
    <Symbol ExecutionSymbol="ATT" Name="AT&amp;T"></Symbol>
    <Symbol ExecutionSymbol="MSFT" Name="Microsoft"></Symbol>
    <Symbol ExecutionSymbol="GOOG" Name="Google"></Symbol>
    <Symbol ExecutionSymbol="CSCO" Name="Cisco"></Symbol>
    <Symbol ExecutionSymbol="IP" Name="International Paper Co."></Symbol>
    <Symbol ExecutionSymbol="MF" Name="MF Global"></Symbol>
    <Symbol ExecutionSymbol="Q" Name="Qwest Communications International Inc."></Symbol>
    <Symbol ExecutionSymbol="BMC" Name="BMC Software Inc."></Symbol>
    <Symbol ExecutionSymbol="WCI" Name="WCI Communities Inc."></Symbol>
    <Symbol ExecutionSymbol="SPY" Name="SDRs"></Symbol>
    <Symbol ExecutionSymbol="LEH" Name="Lehman Brothers Holdings Inc."></Symbol>
    <Symbol ExecutionSymbol="XLF" Name="Financial Select Sector SPDR"></Symbol>
    <Symbol ExecutionSymbol="QQQQ" Name="PowerShares QQQ TR 1"></Symbol>
    <Symbol ExecutionSymbol="IWM" Name="IShare Rus 2000 INDX"></Symbol>
    <Symbol ExecutionSymbol="GE" Name="General Electric Co."></Symbol>
    <Symbol ExecutionSymbol="MER" Name="Merrill Lynch Co., Inc."></Symbol>
    <Symbol ExecutionSymbol="BAC" Name="Bank of America Corporation"></Symbol>
    <Symbol ExecutionSymbol="INTC" Name="Intel Corp"></Symbol>
    <Symbol ExecutionSymbol="F" Name="Ford Motor Co."></Symbol>
    <Symbol ExecutionSymbol="QID" Name="UltraShort QQQ ProShares"></Symbol>
</Symbols>

and we want to load it to a GridView with no server-side code and a quick and easy way. The answer is to use the XmlDataSource object. The XmlDataSource control is an ASP.NET control that allows you to automatically read XML Data and make that data readily available to any ASP.NET control.

To start using this control, go to your Toolbox and drag the XmlDataSource control to your page.

Once the control is on the page it would popup a dialog that has configuration options for our XmlDataSource control. Click the "Configure Data Source" button to configure our XmlDataSource

A popup like below will come up that allows you to select the Xml file you want to your XmlDataSource object to read. It also gives you the option to select the XSL file. You can also specify an XPath expression to use to filter the data in our Xml.

Click the "Browse" button for the "Data File" option to select an XML file.

A new dialog will appear that will let you navigate the folder tree to select your desired XML file

Click "Ok" and you'll be taken back to the "Configure Data Source" screen. Click "Ok" again to finalize the XML data assignment.

Now that we have the file set in to our XmlDataSource control we need assign it to a control. We can do that by dragging a GridView control to our form.

Next, we need to assign the XmlDataSource control as the data source for our GridView. We can do this by selecting our XmlDataSource from the "Choose Data Source" dropdownlist.

Click "XmlDataSource1" and you will notice that our GridView was automatically updated and now shows the contents of our XML file.

Easy huh? Next up, Consuming Web Services in ASP.NET

 

Tuesday, April 01, 2008 12:15:13 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | ASP.NET | Tutorial
# Thursday, March 27, 2008

Last week I showed you ways on how to read text files. This time around we will be looking at how to load XML files into a DataSet.

Let's assume that we have a file called Symbols.xml which contains some XML data.

<?xml version="1.0" encoding="utf-8" ?>
<Symbols>
    <Symbol ExecutionSymbol="ATT" Name="AT&amp;T"></Symbol>
    <Symbol ExecutionSymbol="MSFT" Name="Microsoft"></Symbol>
    <Symbol ExecutionSymbol="GOOG" Name="Google"></Symbol>
    <Symbol ExecutionSymbol="CSCO" Name="Cisco"></Symbol>
    <Symbol ExecutionSymbol="IP" Name="International Paper Co."></Symbol>
    <Symbol ExecutionSymbol="MF" Name="MF Global"></Symbol>
    <Symbol ExecutionSymbol="Q" Name="Qwest Communications International Inc."></Symbol>
    <Symbol ExecutionSymbol="BMC" Name="BMC Software Inc."></Symbol>
    <Symbol ExecutionSymbol="WCI" Name="WCI Communities Inc."></Symbol>
    <Symbol ExecutionSymbol="SPY" Name="SDRs"></Symbol>
    <Symbol ExecutionSymbol="LEH" Name="Lehman Brothers Holdings Inc."></Symbol>
    <Symbol ExecutionSymbol="XLF" Name="Financial Select Sector SPDR"></Symbol>
    <Symbol ExecutionSymbol="QQQQ" Name="PowerShares QQQ TR 1"></Symbol>
    <Symbol ExecutionSymbol="IWM" Name="IShare Rus 2000 INDX"></Symbol>
    <Symbol ExecutionSymbol="GE" Name="General Electric Co."></Symbol>
    <Symbol ExecutionSymbol="MER" Name="Merrill Lynch Co., Inc."></Symbol>
    <Symbol ExecutionSymbol="BAC" Name="Bank of America Corporation"></Symbol>
    <Symbol ExecutionSymbol="INTC" Name="Intel Corp"></Symbol>
    <Symbol ExecutionSymbol="F" Name="Ford Motor Co."></Symbol>
    <Symbol ExecutionSymbol="QID" Name="UltraShort QQQ ProShares"></Symbol>
</Symbols>

And you want to load that data into a DataSet and display it to a GridView. How would you do it?

There are different ways you can load an XML file to a DataSet and i'm here to show you 4 of them. I don't have alot of time to explain the intrinsicts of each sample or performance comparisson of each technique because of time constraints so i'll leave that part for you to figure out the best among this examples. ;)

SAMPLE 1

//Sample 1
//the code below shows us how to load the XML document
//directly to our dataset by just specifying the file
//location to the DataSet.ReadXml method of the DataSet

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //create the Dataset object
            using (DataSet ds = new DataSet())
            {
                //load the xml data to the dataset
                ds.ReadXml(xmlFilePath);

                //bind the values to our GridView
                symbolGridView.Caption = "<h4>Loaded the data using Sample1</h4>";
                symbolGridView.DataSource = ds;
                symbolGridView.DataBind();
            }
        }
    }
}

SAMPLE 2

//Sample 2
//The code below is using the TextReader object to read
//the contents of the file and then assign those values
//to our dataset object

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //Create the TextReader object
            using (TextReader sReader = new StreamReader(xmlFilePath))
            {
                //create the Dataset object
                using (DataSet ds = new DataSet())
                {
                    //load the xml data from the TextReader to the Dataset
                    ds.ReadXml(sReader);

                    //bind the values to our GridView
                    symbolGridView.Caption = "<h4>Loaded the data using Sample2</h4>";
                    symbolGridView.DataSource = ds;
                    symbolGridView.DataBind();
                }
            }
        }
    }
}

SAMPLE 3

//Sample 3
//The code below is basically the same as the above code
//but this time we are using the XmlTextReader object
//instead of using the TextReader object. Please note that
//the XmlReader object calls the TextReader internally.

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //Create the XmlTextReader object
            using (XmlTextReader xTextReader = new XmlTextReader(xmlFilePath))
            {
                //create the Dataset object
                using (DataSet ds = new DataSet())
                {
                    //load the xml data to dataset
                    ds.ReadXml(xTextReader);

                    //bind the values to our GridView
                    symbolGridView.Caption = "<h4>Loaded the data using Sample3</h4>";
                    symbolGridView.DataSource = ds;
                    symbolGridView.DataBind();
                }
            }
        }
    }
}

SAMPLE 4

//Sample 4
//The code below uses an XmlDocument object and reads a file
//using the XmlDocument.LoadXml method. That data is then
//transfered to a DataSet by using the DataSet.ReadXml method
//and passing a XmlNodeReader object based on our XmlDocument

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //Create the Dataset object
            using (DataSet ds = new DataSet())
            {
                //create the XmlDocument object
                XmlDocument xDoc = new XmlDocument();

                //load the contents of the file to our XmlDocument object
                xDoc.LoadXml(File.ReadAllText(xmlFilePath));

                //load the xml data in the XmlDocument object to the Dataset
                ds.ReadXml(new XmlNodeReader(xDoc));

                //bind the values to our GridView
                symbolGridView4.Caption = "<h4>Loaded the data using Sample4</h4>";
                symbolGridView4.DataSource = ds;
                symbolGridView4.DataBind();
            }
        }
    }
}

And that's it. On my next post i'll show you how to load XML data directly to the GridView using the XMLDataSource object.

HTH.

Thursday, March 27, 2008 3:47:22 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial
# Wednesday, March 19, 2008

Sample 3 is similar to Sample 1 and and Sample 2 but it uses the File.ReadAllText method which reads the content of the whole file instead of reading the data line by line or reading the data and putting the values into a string[].

[C# Version]

using System;
using System.IO;

namespace KeithRull.ReadMyFile
{
    class Program
    {
        static void Main(string[] args)
        {
            //the filename of the file to read
            string xmlFilename = "Symbols.xml";
          
            //validate if the file exist
            if (File.Exists(xmlFilename))
            {
                //read the contents of the file
                string fileContent = File.ReadAllText(xmlFilename);

                //display the output in the screen
                Console.WriteLine(fileContent);
            }
            else
            {
                //notify that the file was not found
                Console.WriteLine("File not found!");
            }

            //pause and wait for the user.
            Console.ReadLine();
        }
    }
}

[VB.NET Version]

Imports System 
Imports System.IO 

Namespace KeithRull.ReadMyFile 
    Class Program 
        Private Shared Sub Main(ByVal args As String()) 
            'the filename of the file to read 
            Dim xmlFilename As String = "Symbols.xml" 
            
            'validate if the file exist 
            If File.Exists(xmlFilename) Then 
                'read the contents of the file 
                Dim fileContent As String = File.ReadAllText(xmlFilename) 
                
                'display the output in the screen 
                Console.WriteLine(fileContent) 
            Else 
                'notify that the file was not found 
                Console.WriteLine("File not found!") 
            End If 
            
            'pause and wait for the user. 
            Console.ReadLine() 
        End Sub 
    End Class 
End Namespace 
Wednesday, March 19, 2008 7:18:14 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial
Sample demonstrates how to use the File.ReadAllLines technique 

[C# Version]

using System;
using System.IO;

namespace KeithRull.ReadMyFile
{
    class Program
    {
        static void Main(string[] args)
        {
            //the filename of the file to read
            string xmlFilename = "Symbols.xml";
          
            //validate if the file exist
            if (File.Exists(xmlFilename))
            {
                //read the contents of the file
                string[] fileContent = File.ReadAllLines(xmlFilename);

                //iterate for each line item in our file
                foreach (string lineItem in fileContent)
                {
                    //display the output in the screen
                    Console.WriteLine(lineItem);
                }
            }
            else
            {
                //notify that the file was not found
                Console.WriteLine("File not found!");
            }

            //pause and wait for the user.
            Console.ReadLine();
        }
    }
}

[VB.NET Version]

Imports System
Imports System.IO

Namespace KeithRull.ReadMyFile
    Class Program
        Private Shared Sub Main(ByVal args As String())
            'the filename of the file to read
            Dim xmlFilename As String = "Symbols.xml"
           
            'validate if the file exist
            If File.Exists(xmlFilename) Then
                'read the contents of the file
                Dim fileContent As String() = File.ReadAllLines(xmlFilename)
               
                'iterate for each line item in our file
                For Each lineItem As String In fileContent
                    'display the output in the screen
                    Console.WriteLine(lineItem)
                Next
            Else
                'notify that the file was not found
                Console.WriteLine("File not found!")
            End If
           
            'pause and wait for the user.
            Console.ReadLine()
        End Sub
    End Class
End Namespace

Wednesday, March 19, 2008 7:04:44 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial

Sample 1 demonstrates how to use the StreamReader technique when reading the contents of a file

[C# Version]

using System;
using System.IO;

namespace KeithRull.ReadMyFile
{
    class Program
    {
        static void Main(string[] args)
        {
            //the filename of the file to read
            string xmlFilename = "Symbols.xml";
          
            //validate if the file exist
            if (File.Exists(xmlFilename))
            {
                //create a new StreamReader object tha would read the file
                using (StreamReader sReader = new StreamReader(xmlFilename))
                {
                    //create a string object that would hold the
                    //value of each line in our file
                    string line = String.Empty;
                    
                    //iterate for each line item in our file
                    while ((line = sReader.ReadLine()) != null)
                    {
                        //display the output in the screen
                        Console.WriteLine(line);
                    }
                }
            }
            else
            {
                //notify that the file was not found
                Console.WriteLine("File not found!");
            }

            //pause and wait for the user.
            Console.ReadLine();
        }
    }
}

[VB.NET Version]

Imports System 
Imports System.IO 

Namespace KeithRull.ReadMyFile 
    Class Program 
        Private Shared Sub Main(ByVal args As String()) 
            'the filename of the file to read 
            Dim xmlFilename As String = "Symbols.xml" 
            
            'validate if the file exist 
            If File.Exists(xmlFilename) Then 
                'create a new StreamReader object tha would read the file 
                Using sReader As New StreamReader(xmlFilename) 
                    'create a string object that would hold the 
                    'value of each line in our file 
                    Dim line As String = [String].Empty 
                    
                    'iterate for each line item in our file 
                    While (line = sReader.ReadLine()) IsNot Nothing 
                        'display the output in the screen 
                        Console.WriteLine(line) 
                    End While 
                End Using 
            Else 
                'notify that the file was not found 
                Console.WriteLine("File not found!") 
            End If 
            
            'pause and wait for the user. 
            Console.ReadLine() 
        End Sub 
    End Class 
End Namespace 
Wednesday, March 19, 2008 6:52:53 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial
# Tuesday, February 26, 2008

I've gotten this question 3 times in different flavors since last week and I've decided that it's time to post the solution in my blog. I've added comments on the code to explain what every line is doing.

The code below will demonstrate how you can read the assembly information in both C# and VB.NET.

[------------------ C# ------------------]

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

namespace KeithRull.ReadingAssemblyInformation
{
    /// <summary>
    /// A sample program that demonstrates how to get the
    /// Assembly information. This is useful when you want
    /// to see the version of your class library.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //create a new assembly object that would signify
            //the current running assembly
            Assembly currentAssembly = Assembly.GetExecutingAssembly();

            //create the dictionary object that would contain
            //all the information about our assembly
            Dictionary<string, string> dictionaryOfProperties 
                        = GetAssemblyInfoDictionary(currentAssembly);

            //iterate thru each key in the dictionary
            foreach (string key in dictionaryOfProperties.Keys)
            {
                //print the key and its value
                Console.WriteLine("{0}: {1}", key, dictionaryOfProperties[key]);
            }

            //pause
            Console.ReadLine();

        }

        /// <summary>
        /// A method that returns a Dictionary of Assembly properties
        /// </summary>
        /// <param name="selectedAssembly">The assembly to inspect</param>
        /// <returns>The dictrionary containing the assembly information</returns>
        public static Dictionary<string, string> GetAssemblyInfoDictionary(Assembly selectedAssembly)
        {
            //Create the dictionary
            Dictionary<string, string> dictionaryOfProperties 
                                    = new Dictionary<string, string>();

            //get the location of the assembly
            string assemblyPath = selectedAssembly.Location;
            //add the assembly location to out dictionary
            dictionaryOfProperties.Add("AssemblyLocation", assemblyPath);

            //create the AssemblyName object based on our Assembly
            //this will enable us to get the version information
            //and other properties related to our assembly
            AssemblyName assemblyName = selectedAssembly.GetName();

            //add the FullName of out assembly
            dictionaryOfProperties.Add("AssemblyFullName", assemblyName.FullName);
            dictionaryOfProperties.Add("AssemblyName", assemblyName.FullName);

            //add the assembly version information
            dictionaryOfProperties.Add("Version", assemblyName.Version.ToString());
            dictionaryOfProperties.Add("Version.Major", assemblyName.Version.Major.ToString());
            dictionaryOfProperties.Add("Version.Minor", assemblyName.Version.Minor.ToString());
            dictionaryOfProperties.Add("Version.Build", assemblyName.Version.Build.ToString());
            dictionaryOfProperties.Add("Version.Revision", assemblyName.Version.Revision.ToString());
            
            //add the creation time
            DateTime creationTime = File.GetCreationTime(assemblyPath);
            dictionaryOfProperties.Add("CreationTime", creationTime.ToString());

            //add the last write time
            DateTime lastWriteTime = File.GetLastWriteTime(assemblyPath);
            dictionaryOfProperties.Add("LastWriteTime", creationTime.ToString());

            //return our dictionary obeject
            return dictionaryOfProperties;
        }
    }
}

[------------------ VB.NET ------------------]

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Reflection
Imports System.IO

Namespace KeithRull.ReadingAssemblyInformation
    ''' <summary> 
    ''' A sample program that demonstrates how to get the 
    ''' Assembly information. This is useful when you want 
    ''' to see the version of your class library. 
    ''' </summary> 
    Module Program
        Sub Main(ByVal args As String())
            'create a new assembly object that would signify 
            'the current running assembly 
            Dim currentAssembly As Assembly = Assembly.GetExecutingAssembly()

            'create the dictionary object that would contain 
            'all the information about our assembly 
            Dim dictionaryOfProperties As Dictionary(Of String, String) = GetAssemblyInfoDictionary(currentAssembly)

            'iterate thru each key in the dictionary 
            For Each key As String In dictionaryOfProperties.Keys
                'print the key and its value 
                Console.WriteLine("{0}: {1}", key, dictionaryOfProperties(key))
            Next

            'pause 
            Console.ReadLine()

        End Sub

        ''' <summary> 
        ''' A method that returns a Dictionary of Assembly properties 
        ''' </summary> 
        ''' <param name="selectedAssembly">The assembly to inspect</param> 
        ''' <returns>The dictrionary containing the assembly information</returns> 
        Public Function GetAssemblyInfoDictionary(ByVal selectedAssembly As Assembly) As Dictionary(Of String, String)
            'Create the dictionary 
            Dim dictionaryOfProperties As New Dictionary(Of String, String)()

            'get the location of the assembly 
            Dim assemblyPath As String = selectedAssembly.Location
            'add the assembly location to out dictionary 
            dictionaryOfProperties.Add("AssemblyLocation", assemblyPath)

            'create the AssemblyName object based on our Assembly 
            'this will enable us to get the version information 
            'and other properties related to our assembly 
            Dim assemblyName As AssemblyName = selectedAssembly.GetName()

            'add the FullName of our assembly 
            dictionaryOfProperties.Add("AssemblyFullName", assemblyName.FullName)
            'add the Name of the assembly
            dictionaryOfProperties.Add("AssemblyName", assemblyName.Name)

            'add the assembly version information 
            dictionaryOfProperties.Add("Version", assemblyName.Version.ToString())
            dictionaryOfProperties.Add("Version.Major", assemblyName.Version.Major.ToString())
            dictionaryOfProperties.Add("Version.Minor", assemblyName.Version.Minor.ToString())
            dictionaryOfProperties.Add("Version.Build", assemblyName.Version.Build.ToString())
            dictionaryOfProperties.Add("Version.Revision", assemblyName.Version.Revision.ToString())

            'add the creation time 
            Dim creationTime As DateTime = File.GetCreationTime(assemblyPath)
            dictionaryOfProperties.Add("CreationTime", creationTime.ToString())

            'add the last write time 
            Dim lastWriteTime As DateTime = File.GetLastWriteTime(assemblyPath)
            dictionaryOfProperties.Add("LastWriteTime", creationTime.ToString())

            'return our dictionary obeject 
            Return dictionaryOfProperties
        End Function
    End Module
End Namespace

HTH ;)

 

Tuesday, February 26, 2008 5:16:53 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial
# Monday, February 25, 2008

I've been working on ASP.NET AJAX eversince it's beta days and the UpdateProgress and UpdatePanel has been my bestfriend since day one. I've learned a few tricks while using ASP.NET and today I'd like to share with you several ways to customize the look and feel of you UpdateProgress control with this sample solution. I have created a sample application that would demonstrate different ways to position your UpdateProgress control in the web browser. 

The samples included in the solution is as follows:

  • Sample 1

    The usual ASP.NET AJAX UpdateProgress control usage. This demonstrates the typical use of the UpdateProgress control in an application

  • Sample 2

    An ASP.NET AJAX UpdateProgress control positioned in the top right of the browser. (GMAIL like behavior)

  • Sample 3

    An ASP.NET AJAX UpdateProgress control positioned in the top right of the browser with a transparent gray background. (GMAIL like behavior)

  • Sample 4

    An ASP.NET AJAX UpdateProgress control positioned in the top right of the browser with a transparent gray background and the notification following the scrollbar position (fixed position).

  • Sample 5

    An ASP.NET AJAX UpdateProgress control positioned in the middle of the browser with a transparent gray background.

  • Sample 6

    An ASP.NET AJAX UpdateProgress control positioned in the middle of the browser with a transparent gray background and the notification following the scrollbar position (fixed position).

  • Sample 7

    An ASP.NET AJAX UpdateProgress control that uses the AlwaysVisibleControlExtender trick to place the progress notification at the top right of the browser.

  • Sample 8

    An ASP.NET AJAX UpdateProgress control that uses the AlwaysVisibleControlExtender trick to place the progress notification at the middle of the browser.

The project includes all aspx files, images and stylesheets that demonstrates effective UpdateProgress notifications. You can checkout the running sample of this ASP.NET AJAX project at the DevPinoy.org demo site

I hope what I shared with you can be useful in your daily life as a .NET developer. If you are interested you can download the source code for the whole project here: KeithRull.BuildingABetterAJAXLoadingNotification.zip (191.48 KB)

Monday, February 25, 2008 11:13:42 PM (GMT Standard Time, UTC+00:00)  #    Comments [4] -
.NET | AJAX | ASP.NET | Tutorial
# Monday, February 04, 2008
It's funny, just after posting this post in response to an article that N@rds posted that I realized that I have been preaching about extension methods and his problem was a great example on when to make use of this excellent feature.
Monday, February 04, 2008 6:00:07 PM (GMT Standard Time, UTC+00:00)  #    Comments [2] -
.NET | Fun Stuff | Tutorial
# Friday, February 01, 2008
I wrote an article about this topic a few months ago but I wanted to explain it a little further in this article by showing real-world scenarios on when and how to use this feature. So what does Extension Method mean? According to MSDN: "Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type." Now that we have that settled lets look at a good scenario that depicts a great time when to use an Extension Method.
Friday, February 01, 2008 9:15:19 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Fun Stuff | Tutorial
# Friday, January 25, 2008

I encountered a problem today with one of my projects. The problem was about a third-party application choking when it encounters files greater than 20MB. I have a Windows Service that creates PDFs of KPIs for around 150 users and each of the file that I generate is around 25MB each. I can compress the PDF file to lower resolution format but the management decided that we shouldn't trade quality over speed so I was stuck thinking of a different solution.

Then it came to me that I can zip the files so that I get a smaller file.

I started evaluating C# zip libraries and ended up choosing #ZipLib from the creators of SharpDevelop because of it's simplicity and robustness. The only thing I didn't like was the fact that I wasn't find a great documentation about the library. I ended up fiddling with it. It wasn't hard but you need to play with it alot to understand whats happening in the background.

Ok, now lets start by identifying what we need to create a zip file with SharpZipLib. The first thing that you need to do is download the binaries from the #ZipLib website and reference that DLL in your project.

Now that is done you need to add the ICSharpCode.SharpZipLib namespace in you source file to be able to start creating zip files using the #ZipLib library.

To start creating a zip file, all you need to do is to initialize a ZipFile object.

//initialize the file so that it can accept updates
ZipFile z = ZipFile.Create(filename);

The code above creates a new zip file with the specified filename. Next we need to call the BeginUpdate method of the ZipFile object to tell it that we are going to do some modifications to that file.

//initialize the file so that it can accept updates
z.BeginUpdate();

At this stage we can now add files to our zip file.

//add your file e.g "c:\reports\gummywhammyyummy.pdf"
z.Add(<Your file to add including it's path>);

Once you are done you need to commit  your changes and close the file.

//commit the update once we are done
z.CommitUpdate();
//close the file
z.Close();

Done. Now you have created your first zip file using #ZipLib. Neat! Below is the complete code listing of what we did above

/// <summary>
/// A method that creates a zip file
/// </summary>
/// <param name="zipFileStoragePath">the storage location</param>
/// <param name="zipFileName">the zip file name</param>
/// <param name="fileToCompress">the file to compress</param>
private void CreateZipFile(string zipFileStoragePath
    , string zipFileName
    , string fileToCompress)
{
    //create our zip file
    ZipFile z = ZipFile.Create(zipFileStoragePath + zipFileName);

    //initialize the file so that it can accept updates
    z.BeginUpdate();

    //add the file to the zip file
    z.Add(fileToCompress);

    //commit the update once we are done
    z.CommitUpdate();
    //close the file
    z.Close();
}

But what if you want to add a directory to your zip file? The ZipFile object provides a method called AddDirectory() that accepts a parameter directoryName. The problem with this method is that it doesn't add the files inside the specified directory but instead just creates a directory inside the zip file. To make this work, you need to get the files inside that directory by looping thru all objects in that directory and adding them one at a time. I was able to accomplish this task by creating a recursive function that drills through the whole directory structure of the folder you want to zip. Below is a snippet of the function.

/// <summary>
/// Creates a zip file
/// </summary>
/// <param name="zipFileStoragePath">where to store the zip file</param>
/// <param name="zipFileName">the zip file filename</param>
/// <param name="fileToZip">the file to zip</param>
/// <returns>indicates whether the file was created successfully</returns>
private bool CreateZipFile(string zipFileStoragePath
    , string zipFileName
    , FileInfo fileToZip)
{
    return CreateZipFile(   zipFileStoragePath
                        ,   zipFileName
                        ,   (FileSystemInfo)fileToZip);
}

/// <summary>
/// Creates a zip file
/// </summary>
/// <param name="zipFileStoragePath">where to store the zip file</param>
/// <param name="zipFileName">the zip file filename</param>
/// <param name="directoryToZip">the directory to zip</param>
/// <returns>indicates whether the file was created successfully</returns>
private bool CreateZipFile(string zipFileStoragePath
    , string zipFileName
    , DirectoryInfo directoryToZip)
{
    return CreateZipFile(   zipFileStoragePath
                        ,   zipFileName
                        ,   (FileSystemInfo)directoryToZip);
}

/// <summary>
/// Creates a zip file
/// </summary>
/// <param name="zipFileStoragePath">where to store the zip file</param>
/// <param name="zipFileName">the zip file filename</param>
/// <param name="fileSystemInfoToZip">the directory/file to zip</param>
/// <returns>indicates whether the file was created successfully</returns>
private bool CreateZipFile(string zipFileStoragePath
    , string zipFileName
    , FileSystemInfo fileSystemInfoToZip)
{
    return CreateZipFile(   zipFileStoragePath
                        ,   zipFileName
                        ,   new FileSystemInfo[] 
                            { 
                                fileSystemInfoToZip 
                            });
}

/// <summary>
/// A function that creates a zip file
/// </summary>
/// <param name="zipFileStoragePath">location where the file should be created</param>
/// <param name="zipFileName">the filename of the zip file</param>
/// <param name="fileSystemInfosToZip">an array of filesysteminfos that needs to be added to the file</param>
/// <returns>a bool value that indicates whether the file was created</returns>
private bool CreateZipFile(string zipFileStoragePath
    , string zipFileName
    , FileSystemInfo[] fileSystemInfosToZip)
{
    // a bool variable that says whether or not the file was created
    bool isCreated = false;

    try
    {
        //create our zip file
        ZipFile z = ZipFile.Create(zipFileStoragePath + zipFileName);
        //initialize the file so that it can accept updates
        z.BeginUpdate();
        //get all the files and directory to zip
        GetFilesToZip(fileSystemInfosToZip, z);
        //commit the update once we are done
        z.CommitUpdate();
        //close the file
        z.Close();
        //success!
        isCreated = true;
    }
    catch (Exception ex)
    {
        //failed
        isCreated = false;
        //lets throw our error
        throw ex;
    }
 
    //return the creation status
    return isCreated;
}

/// <summary>
/// Iterate thru all the filesysteminfo objects and add it to our zip file
/// </summary>
/// <param name="fileSystemInfosToZip">a collection of files/directores</param>
/// <param name="z">our existing ZipFile object</param>
private void GetFilesToZip(FileSystemInfo[] fileSystemInfosToZip, ZipFile z)
{
    //check whether the objects are null
    if (fileSystemInfosToZip != null && z != null)
    {
        //iterate thru all the filesystem info objects
        foreach (FileSystemInfo fi in fileSystemInfosToZip)
        {
            //check if it is a directory
            if (fi is DirectoryInfo)
            {
                DirectoryInfo di = (DirectoryInfo)fi;
                //add the directory
                z.AddDirectory(di.FullName);
                //drill thru the directory to get all
                //the files and folders inside it.
                GetFilesToZip(di.GetFileSystemInfos(), z);
            }
            else
            {
                //add it
                z.Add(fi.FullName);
            }
        }
    }
}

A sample usage of this function would be like this:

//sample usage for zipping a file
private void zipItButton_Click(object sender, EventArgs e)
{
    string fileToZip = fileToZipTextBox.Text;
    string zipFilename = Guid.NewGuid() + ".zip";

    //zip a file
    bool isCreated = CreateZipFile(
                                        CONST_ZIP_FILE_STORAGE
                                    ,   zipFilename
                                    ,   new FileInfo(fileToZip) );

    if (isCreated) {
        MessageBox.Show(zipFilename);
    }
    else {
        MessageBox.Show("Failed");
    }
}

//sample usage for zipping a directory
private void zipFolderButton_Click(object sender, EventArgs e)
{
    string directoryToZip = whereToZipTextBox.Text;
    string zipFilename = Guid.NewGuid() + ".zip";

    //zip a directory
    bool isCreated = CreateZipFile(
                            CONST_ZIP_FILE_STORAGE
                        , zipFilename
                        , new DirectoryInfo(directoryToZip) );

    if (isCreated){
        MessageBox.Show("Created " + zipFilename);
    }
    else{
        MessageBox.Show("Failed");
    }
}

And that's it. I hope i was able to shed light on you can create zip files in few easy steps with C#. If you are interested you can checkout this sample application that I built to demonstrate how to create zip files with C# and SharpZipLib.

Download the source code here. KeithRull.CreatingZipFiles.zip (19.15 KB)

Friday, January 25, 2008 8:59:16 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Fun Stuff | Tutorial
# Wednesday, January 23, 2008

Yesterday I wrote an article explaning what var means in .NET 3.x. Today lets talk about Anonymous Types.

Anonymous Types as describe by the C# 3.0 specification are tuple types automatically inferred and created from object initializers. Anonymous Types allows the new operator to be used with an anonymous object initializer to create an object at compile time. The format for an anonymous type declaration is a follows

var v = new { p1 = e1 , p2 = e2 , ... px = ex }

where v is the var variable, px denotes the property name and ex is equaivalent value.

Enough with the theory and lets look at some actual code. Lets assume that I have a class called Person with the following properties:

public class Person
{
    private string _name;
    private int _age;
    private bool _believesInJesus;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }

    public bool BelievesInJesus
    {
        get { return _believesInJesus; }
        set { _believesInJesus = value; }
    }
}

Let's say that I want to create an Anonymous Type that has a similar structure from what we have above, all I need to do is make this call:

var human = new { Name = "Keith", Age = 25, BelievesInJesus = true };

What happens in compile time is that the .NET compiler generates a class that represents the structure of your Anonymous Type.

public class __Anonymous1
{
    private string _name;
    private int _age;
    private bool _believesInJesus;

    public string Name { get { return _name; } set { _name = value; } }
    public int Age { get { return _age; } set { _age = value; } }
    public bool BelievesInJesus { get { return _believesInJesus; } set { _believesInJesus = value; } }
}

It's an  "Anonymous Type" because it a nameless class, it's generated by the compiler for you and it directly inherits from object.

To prove this let's look at this example:

class Program
{
    static void Main(string[] args)
    {
        var person = new { Name = "Keith", Age = 25, BelievesInJesus = true };

        Console.WriteLine(person.Name);
        Console.WriteLine(person.GetType());
        Console.ReadLine();
    }
}

Hovering at the person variable would give us some information about it's type:

The code above will have this output:

As you can see, the compiler created an Anonymous Type Called <>f__AnonymousType0`3. One thing to note about Anonymous Types is that the compiler is smart enough to figure out if an Anonynous Type has already been declared that meets the schema requirement of your new Anonymous Type.

    class Program
    {
        static void Main(string[] args)
        {
            var person1 = new { Name = "Keith", Age = 25, BelievesInJesus = true };
            var person2 = new { Name = "Charissa", Age = 23, BelievesInJesus = true };
            var person3 = new { Name = "Peter", Age = 23};

            Console.WriteLine(person1.Name);
            Console.WriteLine(person1.GetType());
            Console.WriteLine(person2.Name);
            Console.WriteLine(person2.GetType());
            Console.WriteLine(person3.Name);
            Console.WriteLine(person3.GetType());
            Console.ReadLine();
        }
    }

The result would be:

I hope I was able to show you what Anonymous Types are in C#. Next up is Extension Methods.

Wednesday, January 23, 2008 7:58:09 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Fun Stuff | Tutorial
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Keith Rull
Sign In
Statistics
Total Posts: 271
This Year: 0
This Month: 0
This Week: 0
Comments: 182
Themes
Pick a theme:
All Content © 2010, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)