Posts

Showing posts from 2014

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.Valid

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.

ASP.NET 2.0 DropDownList EnableViewState

In ASP.NET 2.0 the  DropDownList   EnableViewState  doesn't work the same as other controls like GridView . Fortunately Anson Goldade created one that does. You can learn how here:  http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet.webcontrols/2006-02/msg00149.html

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

Image
If your S ystem .D iagnostic 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.

Where are UnityContainerExtensions

Image
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 My WebService .My ServiceClient 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 My WebService .My ServiceClient = Nothing Try proxy = New My