Archive for the ‘ASP.NET (C#)’ Category

Enums dont implement IEnumerable

9 May, 2008

 

I know it’s obvious, but you must admit it’s still a little funny :-)

 

Enum

10 ASP.NET Performance and Scalability Secrets

5 February, 2008

An article by Omar Al Zabir explains 10 easy ways to make ASP.NET and AJAX web sites faster, more scalable and support more traffic at lower cost. Omar Al Zabir is the CTO of Pageflakes one of the best mashups.

Main points he has discussed in this article

  • ASP.NET pipeline optimisation
  • ASP.NET process configuration optimisation
  • Things you must do for ASP.NET before going live
  • Content Delivery Network
  • Caching AJAX calls on browser
  • Making best use of Browser Cache
  • On demand progressive UI loading for fast smooth experience
  • Optimise ASP.NET 2.0 Profile provider
  • How to query ASP.NET 2.0 Membership tables without bringing down the site
  • Prevent Denial of Service (DOS) attack

Read the full article at code project 10 ASP.NET Performance and Scalability Secrets

System.Data.DataSetExtensions Config Error in Visual Studio 2008 RTM

3 December, 2007

I got the exception shown below when I opened an application that was developed under VS 2008:

ASP.NET runtime error: Could not load file or assembly ‘System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ or one of its dependencies. The system cannot find the file specified

What’s happened is there is no more an assembly System.Data.DataSetExtensions with version number 2.0. There is a new version for the same assembly name under 3.5.0.0.

To resolve the problem, simply right-click on the web application, properties, then remove the reference to the 2.0.0.0 assembly.

Then, right-click on the web application solution, Add Reference, locate the System.Data.DataSetExtensions and add a reference to it. You may also need to update the entry web / app.config file under the <assemblies> tag. Update the version to 3.5.0.0

Flickr .Net API

26 November, 2007

 
Ran into a few permissions problems with this over the weekend when I came to deploying it with ASP.NET

FlickrNet. It is not incredibly web friendly – you need to override where it looks to store its cache. In your root web.config, you need to add the following section to the configSections part, or if there is no configSection piece, add it to the top of the web.config, right after the configuration section starts:

<configSections>
    <section name=”flickrNet” type=”FlickrNet.FlickrConfigurationManager,FlickrNet” />
</configSections>

And then add this line within the configuration section:

<flickrNet cacheLocation=”e:\domains\qgyen.net\temp” />

There are a few other issues. First, if you are running under medium trust, with the default configuration, you cannot define configuration sections. Second, if you are in medium trust, the cache location will need to be somewhere within your application’s directories. Medium trust often doesn’t let you read or write outside of the application. Third, you cannot use a string like “~/temp” you need to specify the full path.

I’m in a podcast!

30 July, 2007

Have a listen for yourself http://www.craigmurphy.com/blog/?p=610

It was a sound bite taken by Craig after the DeveloperDeveloperDeveloper event held at MS HQ in Reading.

C# Event handling – Using Delegates and Events.

6 June, 2007

There’s no doubt about it wiring an event through classes in VB.NET is much simpler than C#. All VB requires an object reference and a simple ‘handles’ statement on the end of the method declaration.

A couple of friends have asked about wiring C# events through classes so I have put together an example which is available for download. Don’t forget, if you’re wiring events (custom or otherwise), from UI controls you can use ‘AutoEventWireup’ which will make things much easier.

Right click and ‘Save Target As’ to download, then you will need to rename the extension to .zip

Right click here to download.

Overloading routines in WebServices

19 January, 2007

Web services are also classes just like any other .NET classes. Since a web service is a class it can utilise all the OO features like method overloading. However to use this feature with WebMethods we need toadd a little extra syntax.

Creating WebMethods
Create a WebService that has the following overloaded methods:

public string GetGreeting()
public string GetGreeting(string p_Name)
public string GetGreeting(string p_Name, string p_Message)

All these three methods return variants of a Greeting message. Now mark the methods as Web Methods, simply add the [WebMethod] attribute.

[WebMethod]
public string GetGreeting()
{
return “Hi Guest”;
}

[WebMethod]
public string GetGreeting(string p_Name)
{
return “Hi ” + p_Name + “!”;
}

[WebMethod]
public string GetGreeting(string p_Name, string p_Message)
{
return “Hi ” + p_Name + “!” + p_Message;
}

Run the WebService in the browser. That should give an error saying that the GetGreeting() methods use the same message name ‘GetGreeting’.

Adding the MessageName property.
Add the MessageName property to the WebMethod attribute as shown below:

[WebMethod]public string GetGreeting()
{
return “Hi Guest”;
}
[WebMethod (MessageName="WithOneString")]
public string GetGreeting(string p_Name)
{
return “Hi ” + p_Name + “!”;
}
[WebMethod (MessageName="WithTwoStrings")]
public string GetGreeting(string p_Name, string p_Message)
{
return “Hi ” + p_Name + “!” + p_Message;
}

Now compile the WebService and run in the browser. You can see that the first method is displayed as GetGreeting but for the second and third method the alias we set using the MessageName property is displayed.

You could of course use the alias’ as your method names – but that’d be taking the fun out of OO :-)

Failed to access IIS metabase

22 November, 2006

If you have both ASP.NET 1.1 and 2.0 on your machine you may encounter this issue. Upon running your website (via http://localhost/xxxx), you may encounter the error page that reports:

Failed to access IIS metabase

If so, you may have installed IIS after installing the .NET framework. If that’s the case, try repairing your ASP.NET installation. You can do this in one of two ways:

  1. Control panel -> add remove programs -> .net install -> chnage/remove -> repair.
  2. At a command prompt -> aspnet_regiis -i

If however you’re like me and installed VS 2003 / VS 2005 after IIS follow these steps:

  1. Check that the appropriate ASP.NET version is associated with the virtual directory (Select VDIR –> Properties –> ASP.NET tab).

String.Format – a useful reference

13 November, 2006

The String.Format routine can be very powerful – especially with numerics & padding. Here are some examples which I find a handy reference.

int neg = -10;
int pos = 10;

// C or c (Currency)
String.Format(“{0:C4}”, pos); //”$10.0000″
String.Format(“{0:C4}”, neg); //”($10.0000)”

// D or d (Decimal) – leading zeros
String.Format(“{0:D4}”, pos); //”0010″
String.Format(“{0:D4}”, neg); //”-0010″

// E or e (Exponential)
String.Format(“{0:E4}”, pos); //”1.0000E+001″
String.Format(“{0:E4}”, neg); //”-1.0000E+001″

// F or f (Fixed-point)
String.Format(“{0:F4}”, pos); //”10.0000″
String.Format(“{0:F4}”, neg); //”-10.0000″

// P or p (Percent)
String.Format(“{0:P4}”, pos); //”1,000.0000%”
String.Format(“{0:P4}”, neg); //”-1,000.0000%”

XML best practice reminders

18 September, 2006

Example Document

<?xml-stylesheet type="text/xsl" href="name.xsl"?>
<!-- Aaron Skonnard's name structure -->
<x:name xmlns:x="http://example.org/name">
<first>Aaron</first>
<!-- middle initial optional -->
<last>Skonnard</last>
</x:name>
<!-- end of name -->

Processing Non-Repeating Elements

One way is to call Read to move past the text node and then ReadEndElement to consume the element’s end tag marker. The pattern for dealing with text-only elements is shown here:

r.ReadStartElement("first");
Console.WriteLine("first: {0}", r.Value);
r.Read(); // moves past text node
r.ReadEndElement(); // first

This pattern can be summarized into the following steps:

  1. Call ReadStartElement to consume the start tag
  2. Retrieve the element’s text content through the Value property
  3. Call Read to move off the text node
  4. Call ReadEndElement to consume the end tag

To simplify using this pattern, the designers introduced another helper method called ReadElementString that encapsulates this behavior. Using ReadElementString makes it possible to simplify the code even further:

XmlTextReader r = new XmlTextReader("name.xml");
r.ReadStartElement("name", "http://example.org/name");
Console.WriteLine("first:{0}", r.ReadElementString("first"));
Console.WriteLine("last: {0}", r.ReadElementString("last"));
r.ReadEndElement(); // name

You’d be hard-pressed to simplify the code more than this.

Repeating Elements

Dealing with repeating elements also presents a problem since you have to check the name of the next element before committing to the ReadStartElement or ReadElementString call. The following code illustrates how to process the name element assuming it may contain zero or more first elements followed by a mandatory last element:

XmlTextReader r = new XmlTextReader(@"name.xml");
r.ReadStartElement("name", "http://example.org/name");
bool more=true;
while (more)
{
r.MoveToContent();
if (r.LocalName.Equals("first"))
Console.WriteLine("first: {0}",
r.ReadElementString("first"));
else
more=false;
}
Console.WriteLine("last: {0}", r.ReadElementString("last"));
r.ReadEndElement(); // name