DbEntityValidationException

When calling SaveChanges on an entity context object you can sometimes get a DbEntityValidationException error but it doesn't give you a convenient message as to what the error(s) were.  Here is a code snippet to get a convenient message from the DbEntityValidationException object:

Try 
    context.SaveChanges() 
Catch ex As DbEntityValidationException
    'Capture all default Entity Framework Validation errors like required fields, text size checks etc. 
    Dim errorMessage As New StringBuilder

    For Each validationResult As DbEntityValidationResult In ex.EntityValidationErrors
        errorMessage.AppendLine(String.Format("Entity of type [{0}] in state [{1}] has the following validation errors:",  
                                              validationResult.Entry.Entity.GetType.Name(),  
                                              validationResult.Entry.State)) 
        For Each item As DbValidationError In validationResult.ValidationErrors
            errorMessage.AppendLine(String.Format("- Property: [{0}], Error: [{1}]",  
                                                  item.PropertyName,  
                                                  item.ErrorMessage)) 
        Next 

    Next 

    Throw New DbEntityValidationException(errorMessage.ToString, ex)
End Try 

Page.ClientScript.RegisterStartupScript not working

Had a website with a master page and in the the page_load event I was calling Page.ClientScript.RegisterStartupScript to run some javascript based on the current state of the application. In the default.aspx page the script worked fine but in this other page the script was not getting called at all.  

After much research I have learned that Page.ClientScript.RegisterStartupScript only works if the page has a form element that has the runat="server" set.  Once I added a form element with runat="server" to the other page the script finally started getting called the same as it did on the default.aspx page which of course had already had a form element.

Add Charset and ContentType to ASP.NET web page

The WC3 recommends specifying the Charset and ContentType on all of your web pages. A quick and easy way to do this in a ASP.NET web site with a master page is to add this to the masterpage file:


<%  Response.Charset = "utf-8"%> 
<%  Response.ContentType = "text/html"%> 

system.diagnostic trace not creating file in asp.net

If your System.Diagnostic trace is not creating a file in asp.net make sure the the user that the website/webapp is running under has write permissions to the log file folder(default is web root ie where the web.config is).

You can find the user that the website/webapp is running under by going into IIS. Go to the application pool that the website/webapp is running under and the Identity column will show the user that needs write permissions to the log file folder.

Application Pools Grid

Where are UnityContainerExtensions

I had wrapped a Unity container in another class and then was using it to call Resolve and only got one option in intellisense like this:


But I was wanting the Resolve(Of T) option like this:


Then I discovered I was missing "Imports Microsoft.Practices.Unity" at the top of the class.  Once I added that then the UnityContainerExtensions became available.

How to upgrade a web service to ASP.NET 4.0 with out affecting other services

After you have installed .NET 4.0 on the server open a command window and run:

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -norestart -s W3SVC/ID/root/

Replace ID in the line above with the Identifier of the Web Service.  The Identifier can be found by starting the IIS snap in and clicking on the "Web Sites" folder and looking in the right pane.  

How to correctly call a web service in .NET

I came across a problem with calling web services that I had never heard about before. I was creating the client with a Using statement like so:

       Dim xml As XDocument = TestXml 

        Dim actual() As Byte 

        Using proxy As New MyWebService.MyServiceClient 
            actual = proxy.GenerateForm(xml.Root) 
        End Using 

        Assert.IsNotNull(actual) 
This is apparently a no no, as it can mask the real error.  For me I was getting this error:

"System.ServiceModel.ChannelFactory`1[MyLibrary.PNetTServices.PNetTServicesSoap],  cannot be used for communication because it is in the Faulted state."

When in reality when I switched to the recommended client call(http://msdn.microsoft.com/en-us/library/aa355056.aspx):
       Dim xml As XDocument = MyXml 

        Dim actual() As Byte 

        Dim proxy As MyWebService.MyServiceClient = Nothing 
        Try 
            proxy = New MyWebService.MyServiceClient 
            actual = proxy.GenerateForm(xml.Root) 
            proxy.Close() 

        Catch ex As TimeoutException 
            proxy.Abort() 
            Throw ex 'Or do what ever 
        Catch ex As CommunicationException 
            proxy.Abort() 
            Throw ex 
        Catch ex As Exception 
            proxy.Abort() 
            Throw ex 
        End Try  

I got the real error message which was that in the config binding maxBufferSize cannot be a different value from maxReceivedMessageSize.