ASP.net MVC

45 posts

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 […]

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 […]

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 […]

How to fix problems with Linq and the Entity Framework

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 […]

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 […]

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) { @Request.Cookies[“NotifyBar”].Value.ToString() X var c = new HttpCookie(“NotifyBar”); c.Expires = DateTime.Now.AddDays( -1 ); Response.Cookies.Add( c ); } 2.  Put this […]

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

The 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 […]

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 […]

How to fix the copy file problem in C#

photo © 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 […]

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

photo © 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 […]