ASP.net Archives - Page 2 of 4 - Digital Tool Factory blog ASP.net Archives - Page 2 of 4 - Digital Tool Factory blog

The Digital Tool Factory Blog

How to fix yet another You must set this property to a non-null value of type ‘Double’ problem with Entity Framework

The Problem

You are coding happily away in Visual Studio 2010, working on your asp.net project, using the entity framework and sql server and you get and you get the following error

The ‘Amount’ property on ‘SomeTable’ could not be set to a ‘Decimal’ value. You must set this property to a non-null value of type ‘Double’.

What you say?  Why do I have to set it to non-nullable? It looks something like

public double? Amount { get; set; }

The Cause

The root problem is in your database schema  – something in how that table is set up is not gibing correctly with entity framework.

The Solution

Just set the property to a non-nullable decimal type, for example

public decimal Amount { get; set; }

It’s not a perfect solution, but Entity Framework does not like all sql server data types, and I have yet to find an explanation as to why or a list of things to avoid.  If anyone does know please leave a link in the comments.  This is a persistent problem for me when working with ourside databases, and I’m sure I’m not the only this happens to.  Your input would be quite helpful here Microsoft.  It would be a nice improvement in the next version of Entity Framework.


13
Mar 12


Written By Steve French

 

How to fix problems with asp.net, C# and Request.ServerVariables[“HTTP_REFERER”]

The Problem: I recently built a small web app that tracked downloads.   Part of that was using the referring page.  Somehow, for some reason, the url was not being sent along with the request.

The Cause: After much travail, I finally noticed that the browser was on page https://www.somedomain.com and the link in question was http://somedomain.com/mytrackingapp – i.e. not under SSL, and with a slightly different hostname.  For some reason the referer header is not sent in that situation.

The Solution: Just change the link to https://www.somedomain.com/mytrackingapp and life is good.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


12
Aug 11


Written By Steve French

 

How to fix problems with Coded User Interface Tests

The Problem:

You attempt to create a new Coded User Interface Test in Visual Studio 2010, and you get the following error:

The following package failed to load: C:Users[File Path Goes Here]Microsoft.VisualStudio.TestTools.UITest.Extension.IE.dll. Coded UI Test is now in an inconsistent state. Remove this package and restart Visual Studio to work with Coded UI Test.

The Cause:

For whatever reason, Visual Studio does not copy Microsoft.VisualStudio.TestTools.UITest.Extension.IE.dll into the solution, and but it thinks it did.

The Solution:

Just copy the file over yourself.  Here’s how.

  1. Close Visual Studio
  2. Go to C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEPrivateAssemblies
  3. Copy the Microsoft.VisualStudio.TestTools.UITest.Extension.IE.dll file into the bin/debug directory
  4. Restart Visual Studio and try again.

No idea on the root cause, but that should fix the problem.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


11
Jul 11


Written By Steve French

 

How to do a cool flash notification message in asp.net mvc 3 in 6 easy steps

I pieced all of this together from various sources online, so the code is a bit rough, but here it is:

1.  Create a partial razor view, call it _NotifyBar.cshtml, it contains this:

@if (Request.Cookies[“NotifyBar”]!=null)
{

var c = new HttpCookie(“NotifyBar”);
c.Expires = DateTime.Now.AddDays( -1 );
Response.Cookies.Add( c );

}

2.  Put this in the header of your _Layout.cshtml file
//here is the jbar stuff

<script type=”text/javascript”>//
$(document).ready(function () {
$(“#message”).fadeIn(2000);
$(“#message”).delay(5000).fadeOut(1000);
$(“#message a.close-notify”).click(function () {
$(“#message”).fadeOut(“slow”);
return false;
});
});
</script>

3.  Right after the body of your _Layout.cshtml page there is this code

@Html.Partial(“_NotifyBar”)

4. Create a file called ExtensionMethods.cs, add in this code

public static ActionResult SetStatusMessage(this ActionResult ar, string str)
{
var c = new HttpCookie(“NotifyBar”);
//c.Expires = DateTime.Now.AddDays(-1);
c.Value = str;
HttpContext.Current.Response.Cookies.Add(c);
return ar;
}

5. Put this in your stylesheet

#message {
font-family:Arial,Helvetica,sans-serif;
position:fixed;
top:0px;
left:0px;
width:100%;
z-index:105;
text-align:center;
font-weight:bold;
font-size:100%;
color:white;
padding:10px 0px 10px 0px;
background-color:#8E1609;
}

#message span {
text-align: center;
width: 95%;
float:left;
}

.close-notify {
white-space: nowrap;
float:right;
margin-right:10px;
color:#fff;
text-decoration:none;
border:2px #fff solid;
padding-left:3px;
padding-right:3px
}

.close-notify a {
color: #fff;
}

6. Then in your controller, just return add “SetStatusMessage” on your RedirectToAction, for example

return RedirectToAction(“Index”).SetStatusMessage(“You have successfully edited the ” + project.ProjectName + ” project.”);

That’s it!  You can now have a fade-out notification message on any page you like.  The use of the cookies is a bit cumbersome, but I could implemnt it quickly.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


24
May 11


Written By Steve French

 

How to Fix the “Service Unavailable” problem in IIS

Gears gears cogs bits n piecesThe Problem: For whatever reason, your website is displaying a white screen with “Service Unavailable” and nothing else.

The Cause: There could be many causes, but the one I just discovered was that the application pool had shut down for no good reason.

The Solution: In IIS, navigate to “Application Pools”, right click to bring up the content manager – select “Stop”, and then select “Start”.  That should fix the problem in most cases.

Creative Commons License photo credit: Elsie esq.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


23
May 11


Written By Steve French

 

How to create a asp.net gridview hyperlink field with multiple querystring parameters

Frozen linksThe Problem: You need to put a relatively complicated link (i.e. the link has more than one parameter) into an asp.net gridview column.

The Cause: No cause really, you just need to know the exact syntax
The Solution:

<asp:HyperLinkField HeaderText=””

Text=”Download Your File”

DataNavigateUrlFields=”CategoryID,FileID”

DataNavigateUrlFormatString=”FileDownloader.

aspx?catid={0}&file={1}” />

You can have as many fields as you like in the DataNavigateUrlFields tag, and it will simply autopopulate from that point on (provided you have the parameters in proper {0} fashion.  This is hardly the biggest problem I’ve ever faced, but I didn’t know how to create the Hyperlink field in that way and now I do.
Creative Commons License photo credit: skedonk

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


19
May 11


Written By Steve French

 

How to fix problems with asp.net mvc 3 charts and razor pages

The Problem: You attempt to use the super-cool new charting features in asp.net mvc 3, and all you get is compilation errors

The Cause: You are using the old school System.Web.UI.DataVisualization.Charting.Chart namespace

The Solution: Delete

using System.Web.UI.DataVisualization.Charting.Chart

and instead insert

using System.Web.Helpers;

If you are using the chart code in an actual razor page (and not in the controller), just insert

@using RazorHelpers.Helpers

into the top of the page

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


06
May 11


Written By Steve French

 

How to fix the asp.net DetailsView data validation problem

Jugando con texturas (XIII): Resurrecciónphoto © 2010 Zyllan | more info ia: Wylio) The Problem: You have the standard asp.net DetailsView control (one of many on that page) and your custom validator refuses to work.  So do all of your standard validators now that you think about it.  Your command button looks like this:

<asp:ButtonField ButtonType="Button" ValidationGroup="Main" Text="Add This" CommandName="InsertNewRecord"  />
and all of your ValidationGroups are set to "Main"

The Cause: DetailsView is designed to work that  way, don’t ask me why

The Solution: Put everything in a regular button, like so:

<asp:TemplateField><InsertItemTemplate>
<asp:Button runat="server" ID="btnAddNewAddress" ValidationGroup="Main" CommandName="InsertNewRecord"  Text="Add This"/>
</InsertItemTemplate>
</asp:TemplateField>
That works, no idea why, but it does.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


02
Mar 11


Written By Steve French

 

How to fix the copy file problem in C#

Cavalry moving forwardphoto © 1918 National Library of Scotland | more info (via: Wylio)The Problem: You need to copy a file from one location on the server to another and cannot remember how.

The Cause: It is too short and simple, and easily forgotten

The Solution: Just use the following code:

public static void CopyAndRenameFile(string OldPath, string OldFileName, string NewPath, string NewFileName)
    {
        if (File.Exists(OldPath + OldFileName))
        {
            File.Copy(OldPath + OldFileName, NewPath + NewFileName, true);
        }
    }

That’s it!

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


06
Dec 10


Written By Steve French

 

How to fix the “Unable to cast object of type” problem

Drill Bitsphoto © 2008 Justin Gurbisz | more info (via: Wylio) The Problem – you have several asp.net web controls (.ascx files) in your website project and for no obvious reason you suddenly get the error “Unable to cast object of type ‘YourWebControlName_ascx’ to type ‘YourWebControlName_ascx’.”  You recompile and the program works, then stops working.  You recompile and it works again, then it stops working, ad infinitum, but the working times become less frequent, and eventually the site stops working at all.

The Cause – ASP.net will cache the wrong things.  In my case I converted the project from a full solution to a regular website and did not remove the compiled files (SolutionName.dll) in the bin and obj directories, which compounded the problem.

The Solution – Delete everything in in the “Temporary ASP.net Files” directory, which (on my machine) was located here C:WindowsMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files.  Then delete everything in obj directory, and the solution files from the bin directory.  That should fit it!

This was more of a stupid error on my part than a great mystery, but problematic nonetheless.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


03
Dec 10


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.