Archive for December, 2010

DropDownList – highlighting an item by value (or text)

I had a DropDownList in an ASP.NET page that held the country values, such as this:

 <asp:DropDownList ID="ddlCountry" runat="server">
                <asp:ListItem Text="United States">United States</asp:ListItem>
                <asp:ListItem Text="Canada">Canada</asp:ListItem>

 The user object held a property called Country. It held – brace yourself – the country where the user lived! I wanted the DropDownList to display the value of the user’s country. After I instantiated (love that word) the user object, I simply called the FindByValue method of the DDL’s Items collection, like so:

 ddlCountry.Items.FindByValue(TheUser.Country).Selected = true;

Leave a comment

Silverlight tutorial series

In the vein of the Twelve Days of Christmas, Jeff Blakenburg has the 31 day of Silverlight. I’ve had some experience with Silverlight, but I’m going through all 31 days, because you never know when you can pick up something useful. The tutorials are well-written and easy to follow.

1 Comment

Global application variables in Silveright

I’ll take the most simple path for creating global variables in Silverlight. In the Appl.xaml.cs file, declare a public variable:

         public string MyValue = "The Lions won a game!";

To get the value of this variable from another XAML code behind file:

         string SomeValue = ((App)Application.Current).MyValue;

Leave a comment

Instance failure

There I was, minding my own business, encrypting my configuration files in the web.config like a good boy, and I got this message when I tried to open a SqlConnection object:

Instance failure

Luckily it didn’t take too long to find out what the problem was. An article on Scott Hanselman’s site showed me the solution. My connection string initially looked fine:

Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Documents and Settings...

But, the problem were the double backslashes were turning into quadruple backslashes when I viewed it in the debugger:

Data Source=.\\\\SQLEXPRESS;AttachDbFilename='C:\\\\Documents and Settings...

That can’t be good. Once I changed the connection string to use single backslashes instead of double, all was well in my world again:

Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings...

Leave a comment

Loading an XML file from a stream

I’ve been working with XML files being returned from the Flickr web service. The authentication scheme for Flickr using their APIs is a little involved, and I’ll get more into that process in a future blog post.

One step in the authentication process is returning a “frob” for a Flickr application. The results of the request are in XML format (unless you choose differently) and look like this:

<rsp stat="ok">

I was doing this asynchronously, and the results were returned as a stream. Here is my finished function, which converts the stream (the parameter e.Result is a stream) into a string and uses XmlDocument.LoadXml to load the XML:

            XmlDocument doc = new XmlDocument();
            StreamReader Reader = new StreamReader(e.Result);
            XmlNodeList Node = doc.GetElementsByTagName("frob");

Afterwards, a simple called to the XmlNodeList.GetElementsByTagName to retrieve the correct tag, and a call to the FirstChild property of the XML node, was all that was needed.

Leave a comment

Solving the “Asynchronous operations are not allowed… ” exception

While doing some asynchronous coding in a code behind file, I ran across a System.InvalidOperationException exception, with this message:

Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.

Luckily, the solution was very easy. I added the tag Async=”true” to the Page declaration, and that was it:

<%@ Page Language="C#" Async="true" ...  %> 

Leave a comment

The Silverlight value converter

Dan Wahlin has a nice article on implementing the IValueConverter interface on his blog. I’ve used value converters in the past and not fully known the intricacies of them, so this was a clear tutorial and explanation of what they are for and how to use them.

Leave a comment