Thursday, January 30, 2014

Encapsulate a string to act as an array of strings


Here is a quick snippet for expose in a class a string that contains values comma-separated (eg: value1,value2,value3) as an array.

    string myString;

    public string[] MyArray
    {
      get
      {
        if (string.IsNullOrEmpty(myString))
          return new string[0];
        return myString.Split(',');
      }

      set
      {
        myString = String.Join(",", value);
      }
    }

Sample input

 myString = "value1,value2,value3";

Result

myArray = string[]{ "value1", "value2", "value3" };

Tips

Be sure that string does not contains extra ',' in order to avoid unwanted splitting.

Submit this story to DotNetKicks

Asp Net config, automatically disable debug on production servers using a nice feature on machine.config

Developing in hurry is not the best thing to do but we all have to.
One of the things that I usually forgot to change in production is turning off the debug mode on web.config
So i forget to change:
<compilation debug ="true">
to
<compilation debug ="false">

So why leaving debug to true is so bad?
  • Compilation takes longer as batch optimizations are disabled 
  • Scripts and images from WebResources.axd will not be cached on the client 
  • Larger memory footprint 
  • Code will execute slower because of enabled debug paths
And you have to multiply for each site you are deplyoing in the same server.
This means wasting more memory, and having sites running much slower.
And you often cannot check every single web.config to see if the settings are right.

When deploying ASP.NET applications to a production environment, to force debug = false in all ASP.NET applications on the server, you can change the retail switch in machine.config:

<configuration>
    <system.web>
        <deployment retail="true" />
    </system.web>
</configuration>

This will also disable page output trace and force custom error pages.
This can be great generally, but can turn in a nightmare if you have a site that does not run properly since you have custom errors always on.
Solutions can be:
  • look at event log of server
  • install an aspnet logger like elmah
  • try debug locally
  • set retail to false for a while (not recommended because causes rebuild of all sites)
Have a nice day folks!

Submit this story to DotNetKicks

Monday, January 27, 2014

Using a Dictionary as the DataSource for a DropDownList

It is possible to use a Dictionary or even Dictionary<string> as the DataSource for a DropDownList directly, without having to iterate through the the Dictionary's KeyValuePair collection and populate the DropDownList's Items collection manually.

The text member is "value" and the value member is "key" when using one of these types of objects to bind to a DropdownList.

Dictionary<byte> dicTable = new Dictionary<byte>();
dicTable.Add(1, "One");
dicTable.Add(2, "Two");
dicTable.Add(3, "Three");
dicTable.Add(4, "Four");
cmbDropDownList.DataSource = dicTable;
cmbDropDownList.DataTextField = "Value";
cmbDropDownList.DataValueField = "Key";
cmbDropDownList.DataBind();

Submit this story to DotNetKicks