ASP.Net MVC And Ninject

04.10.09

Problem: You want to be able use the Ninject frame work with the new and shiny ASP.Net MVC framework.

Solution: Assembly: Ninject.Framework.MVC Contains an Abstract Base class you can Inherit from to do most of your heavy lifting.

Some Code

Global.asax.cs

public class MvcApplication : NinjectHttpApplication
{
      private static IKernel _kernal;
      protected override void RegisterRoutes(RouteCollection routes)
      {                 
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
          routes.MapRoute("Default", // Route name
                          "{controller}/{action}/{id}", // URL with parameters
                          new { controller = "Home", action = "Index", id = "" } // Parameter defaults
                         );
      }                 
     protected override IKernel CreateKernel()
     {
         if (_kernal == null)
         {
             var modules = new IModule[]
             {
                 new AutoControllerModule(Assembly.GetExecutingAssembly()),
                 new DemoModual()
             };                
             _kernal = new StandardKernel(modules);
         }              
         return _kernal;         
      }
}

Unhandled Error in Silverlight 2 Application Code: 2104

02.17.09

Problem: After deploying your silverlight application to your Web server you get the following error.

Error: Unhandled Error in Silverlight 2 Application
Code: 2104   
Category: InitializeError
Message: Could not download the Silverlight application. Check web server settings    
Source File: TestPage.html
Line: 53
 

Solution: The MIME types on your webserver are not setup correctly to host your silver light application. The MIME types needed are:

.xaml    application/xaml+xml
.xap    application/x-silverlight-app
.xbap    application/x-ms-xbap

Hack: If you are like me and have your silverlight hosted on a shared hosting environment like godaddy you cannot change your MIME types. Instead change the .xap to .zip you must also change the source parameter value inside your page to match.

<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="Microsoft.Windows.Controls.Samples.zip"/>
    <param name="onerror" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="2.0.31005.0" />
    <param name="autoUpgrade" value="true" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
    </a>
</object>

Generic List Operations

02.04.09

Problem: You need a quick and easy way to manipulate elements contained in a list.

Solution: Lambda Expression, these expressions are used in the same fashion you would use anonymous methods but are less wordy.

Anonymous methods:

employees.FindAll(delegate(employee e1)
{ 
    return e1.Name == "Dave"; 
})

Lambda Expression:

employees.FindAll(e1 => e1.Name == "Dave");
employees.Sort((e1,e2) => e1.LastName.CompareTo(e2.LastName));

For more complex expression use the syntax like (params) => {statment}.

employees.Sort((e1, e2) =>
{                                      
    if (e1.LastName.CompareTo(e2.LastName) == 0)
    {
        return e1.FirstName.CompareTo(e2.FirstName);
    }
    else
    {
        return e1.LastName.CompareTo(e2.LastName);
    }
});

For more Info: MSDN

Configuration System Failed to Initialize

05.23.08

Problem: Your application contains User Scope Settings and you receive the exception.

“Configuration system failed to initialize” when attempting to retrieve these settings.

Solution: The settings file has been corrupted and is unreadable. Close or stop your application and delete the user.config file. The file is located at:

C:\Documents and Settings\[UserName]\Local Settings\Application Data\[AppCompany]\[AppName] \[AppVersion]\

If you are in Visual Studio you can easily find the exact file that is causing the problem by viewing the details and drilling down to the inner exception to find the file name.

System.Data.EvaluateException

02.01.08

Problem: You are using Select Method to retrieve rows from a typed DataTable and you keep getting the fallowing exception:

System.Data.EvaluateException: Cannot perform '=' operation on System.String and System.Int32.

This isn't the most helpful error message.

Solution: You are missing hyphens.

Example:

Wrong:

CustomerRow[] rows = Select(string.Format("LastName = {0}", LastName)) as CustomerRow[];

Right:

CustomerRow[] rows = Select(string.Format("LastName = '{0}'", LastName)) as CustomerRow[];

System.Diagnostics.Debugger

10.11.07

Problem: You need functionality turned off or on when you are running your program in Debug mode.

Solution: System.Diagnostics.Debugger.IsAttached; it is a static property that returns true if there is a Debugger attached. I like to use this when debugging websevices, timing out while fixing a problem can be annoying.

if (Debugger.IsAttached)
     _webService.Timeout = -1;

More info:

MSDN

 

Removing Non-Numeric Characters from a String

09.21.07

Problem: I have an editable grid that needs to contain numeric integer values only.

My Solution: TryParse, this function accepts a string and an out value then returns a  bool True if it was successful, also it does not throw exceptions. I am a 100% sure there is someone with a better way of doing this but this is what I came up with. Feedback encouraged!

private string RemoveNonNumericValues(string p)
{
     int tempValue = 0;
     StringBuilder value = new StringBuilder(p.Length);
     foreach (char charValue in p.ToCharArray())
     {
         if (int.TryParse(charValue.ToString(), out tempValue))
             value.Append(tempValue.ToString());
     }
     return value.ToString();
}
private void dataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    dataGridView[e.ColumnIndex, e.RowIndex].Value = RemoveNonNumericValues(dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString());
}

Sweet Sweet NUnit on VS2005 Action

09.15.07

This is great! I dont know why I didnt do this earlier, but you can Debug your code and run NUnit at the same time. You just need to set the Debug>Start Action to start external aplication NUnit.exe. Then set your start project to be the same project as your test code and tada you now have debuging and break point goodness wile running nunit.

 

Unable to find the report in the manifest resources. please build the project, and try again.

09.13.07

I was reorganizing a project and I moved a crystal report after that I received this error at runtime "Unable to find the report in the manifest resources. please build the project, and try again." No amount of building could fix this problem. I eventually had to open up the project file in note pad and edit the entries in the xml/MSBUILD.

Here is what a Crystal Report should look like in your MSBUILD script.

<Compile Include="CrystalReport1.cs">
    <AutoGen>True</AutoGen>
    <DesignTime>True</DesignTime>
    <DependentUpon>CrystalReport1.rpt</DependentUpon>
    <SubType>Component</SubType>
</Compile>
<EmbeddedResource Include="CrystalReport1.rpt">
    <Generator>CrystalDecisions.VSDesigner.CodeGen.ReportCodeGenerator</Generator>
    <LastGenOutput>CrystalReport1.cs</LastGenOutput>
</EmbeddedResource>

After you correct the entry Rename the file Build then rename it again to the old name and build again.