Friday, February 14, 2014

The specified solution configuration “Debug|BNB” is invalid

Problem

When you try to build from command prompt or batch file you get the error

‘The specified solution configuration “Debug|BNB” is invalid. Please specify a valid solution configuration using the Configuration and Platform properties (e.g. MSBuild.exe Solution.sln /p:Configuration=Debug /p:Platform=”Any CPU”) or leave those properties blank to use the default solution configuration.

Background

You have an HP computer.
A system wide setting of an environment variable called "Platform" seems to override any other setting.

The issue

At build-time the compiler has to know what the target platform of your code, i.e. the system architecture like X86, 64bit etc. To do so it searches to "Platform" variable first, that is wrong in our case since BNB is not a valid platform for msbuild.

The solution

Delete the conflicting environment variable "Platform" in "Control Panel -- System -- Advanced -- Environment Variables". Restart your computer and try again.

Submit this story to DotNetKicks

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

    Friday, January 27, 2012

    C# getting file names without extensions

    When you walk a directory getting file names you get a file name with its extension: file1.txt, file2.txt, file3.txt.
    How can you get file names without file extensions file1, file2, file3?

    DirectoryInfo d = new DirectoryInfo(myDirectory);
    FileInfo[] fileArrray = d.GetFiles("*.txt");
    foreach (FileInfo f in smFiles)
    {
       var fileNameOnly = Path.GetFileNameWithoutExtension(f.Name);
       
       //...
       //process file...
    }
    

    Thre is also a linq way:

    var filenames = String.Join(", ", Directory.GetFiles(@"c:\", "*.txt")
    .Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray());
    

    Submit this story to DotNetKicks

    Wednesday, January 25, 2012

    Jquery tabs postback problem and solution

    I am using jquery library in my project and I use jquery-ui tabs.
    When a postback occurs in any tab, tabs reloaded and goes to first tab.

    <script type="text/javascript">
        $(document).ready(function() {
            $("#example").tabs();
        });
    </script>
    

    Solution is to store last tab in an hidden field and reload each time page loads.

    <script language="javascript" type="text/javascript">
        $(function() {
            $("#example").tabs({
                show: function() {
                    var sel = $('#example').tabs('select', $("#<%= hidLastTab.ClientID %>").val(sel));
    
                },
                selected: <%= hidLastTab.Value %>
             });
          });
    </script>
    <asp:hiddenfield id="hidLastTab" runat="server" value="0"></asp:hiddenfield>

    or alternatively with a funcion:

    private void SelectTab(int tabNumber) {
                var script = string.Format(@"$(document).ready( function(){{ $('.tabs').tabs( 'select', {0} ); }});", --tabNumber);
                ScriptHelper.RegisterHeadScriptBlock(typeof(System.Web.UI.Page), "TabSelector", script, true);
    
            }
    
    
    

    Submit this story to DotNetKicks

    Thursday, January 19, 2012

    What is a Method Group?

    A method group is the name for a set of methods (that might be just one).
    The ToString function has many overloads - the method group would be the group consisting of all the different overloads for that function.
    It is a compiler term for "I know what the method name is, but I don't know the signature"; it has no existence at runtime, where it is converted in the correct overload.
    Also, if you are using LINQ, you can apparently do something like myList.Select(methodGroup).
    so you can replace this code:
    private static int[] ParseInt(string s)
    {
        var t = ParseString(s);
        var i = t.Select(x => int.Parse(x));
        return i.ToArray();
    }
    with this one:
    private static int[] ParseInt(string s)
    {
        var t = ParseString(s);
        var i = t.Select(int.Parse);
        return i.ToArray();
    }

    Submit this story to DotNetKicks