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

The Digital Tool Factory Blog

How to fix problems with the X-Axis in Microsoft Charting Components

The Problem: You’re creating a line or area chart using the Microsoft Charting components (the ones in System.Web.UI.DataVisualization.Charting, not System.Web.Helpers) and for whatever reason the first entry is actually in the second quadrant, like so:

The Cause: The charting components assume you want to show some sort of contrast for the first value, but it just doesn’t look right in this case.

The Solution: Set your minimum value to 1 instead of zero (zero is the default), like so

myChart.ChartAreas[0].AxisX.Minimum = 1;

The chart will now be pushed flush left, for a much better appearance.  You will see something like the chart below.

 

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


01
Sep 11


Written By Steve French

 

How to fix problem with asp.net mvc 3 and json

The Problem: You are attempting to import json data into your web application, but you continue to get weird errors.  Here is what the json data looked like

[{“entry”:{“tags”:[{“name”:”Web Development”,”billable”:true,”id”:117906}],”created_at”:”2011-02-04T01:00:28Z”,”billable”:true,”minutes”:150,”updated_at”:”2011-02-04T01:00:40Z”,”recently_updated_at”:”2011-02-04T01:00:40Z”,”project_id”:40401,”import_id”:null,”url”:null,”time_to”:null,”id”:670893,”date”:”2011-02-02″,”user_id”:12914,”formatted_description”:””,”description_text”:””,”time_from”:null,”description”:”Web Development”,”invoiced_at”:null,”project_invoice_id”:null}},{“entry”:{“tags”:[{“name”:”Web Development”,”billable”:true,”id”:117906}],”created_at”:”2011-02-04T01:04:42Z”,”billable”:true,”minutes”:180,”updated_at”:”2011-02-04T01:04:42Z”,”recently_updated_at”:”2011-02-04T01:04:42Z”,”project_id”:40401,”import_id”:null,”url”:null,”time_to”:null,”id”:670909,”date”:”2011-02-01″,”user_id”:12914,”formatted_description”:””,”description_text”:””,”time_from”:null,”description”:”Web Development”,”invoiced_at”:null,”project_invoice_id”:null}},{“entry”:{“tags”:[{“name”:”Web Development”,”billable”:true,”id”:117906}],”created_at”:”2011-02-04T01:11:13Z”,”billable”:true,”minutes”:60,”updated_at”:”2011-02-04T01:11:13Z”,”recently_updated_at”:”2011-02-04T01:11:13Z”,”project_id”:40401,”import_id”:null,”url”:null,”time_to”:null,”id”:670914,”date”:”2011-01-31″,”user_id”:12914,”formatted_description”:””,”description_text”:””,”time_from”:null,”description”:”Web Development”,”invoiced_at”:null,”project_invoice_id”:null}}]

The Cause: The json is not in the format you think it should be in  While it looks like you would get each record back, you get one solid blob.

The Solution: Just remove the “[{“entry”:” and final “}”.  Ordinarily I wouldn’t make a big deal of this, but it did take me more than 30 minutes to figure out the cause of the problem, so here it is.

 

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


15
Jul 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 fix problems with Linq and the Entity Framework

database 2

The Problem:

You attempt to do a query using Linq To Entities and your code does not work.

The Cause:

For reasons unknown, Linq to Entities has different operators than Linq To Sql.

The Solution:

Call your initial query/pull and us .ToList() on it before you run any of the problem operators – this fixes the problem entirely.
I am embarrassed to say that I spent 30 minutes trying to get Linq to Entities to work directly before I thought of that.

Creative Commons License photo credit: Tim Morgan

 

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
Jun 11


Written By Steve French

 

How to fix problems uploading images in asp.net mvc 3/razor

The Problem: You are attempting to create create an asp.net mvc 3 page with razor that uses an upload control.  You try to upload an image and all you get is a “server not available” error.  Not very descriptive it it?

The Cause: ASP.net sets the default upload limit very low.

The Solution: Raise the limit, just add this to the web.config file ,

<httpRuntime maxRequestLength=”9097151″ executionTimeout=”3600″/>

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


13
Jun 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 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 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.