Friday, February 20, 2015

Server.MapPath in MVC - HostingEnvironment.MapPath

Server.MapPath is throwing error in MVC? use HostingEnvironment.MapPath instead.

HostingEnvironment.MapPath needs to include to use System.Web.Hosting.

Following is the sample code:

var file = System.IO.File.AppendText(System.Web.Hosting.HostingEnvironment.MapPath("~/logs/error.txt"));

Tuesday, February 10, 2015

@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" }) object routevalues decoded

@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })
4th Parameter in @Html.ActionLink is routeValues and of object type. This can be used to set query string and manage resulting anchor tag.

Following line will render an anchor tag with 3 query string parameters:
@Html.ActionLink("Application name", "Index", "Home", new { page = 1,name="my name",gogs="ola" }, new { @class = "navbar-brand" })

Result: http://localhost:34446/Home/Index/1?name=my%20name&gogs=ola



Similarly following code will return an anchor tag with id in url(not in query string parameter):

@Html.ActionLink("Application name", "Index", "Home", new { id = 1 }, new { @class = "navbar-brand" })

Result: http://localhost:34446/Home/Index/1

MVC: Cannot create/shadow copy 'System.Web.Mvc' when that file already exists

This is a temporary build error. Following steps would help you resolve this error:


  1. Wait for some time(30 sec to 1 min).
  2. Right click Solution and clean solution.
  3. Rebuild Solution.
  4. Open some other url in browser bar.
  5. Try reloading same url again.
Hope this resolves your error. However if this still does not work for you, then go to temp location (asp.net temp files in program files) and delete all the project related temporary files(dlls). And rebuild solution. You might need to close visual studio as few files could be in use.


This should resolve the error. If you face such errors very frequently, I would recommend you to clear your machine. Delete all the temp files. empty recycle bin, delete unnecessary files from system drive(c drive), clean desktop.

And you still face such errors(and other like visual studio is busy performing some internal operation), in worst case you might need to fix your windows.(if you are unable to do this, get your machine formatted if possible).

Monday, February 9, 2015

asp.net MVC 5 on Visual Studio 2012 (Update Visual Studio)

Please install following tools to update MVC in Visual Studio 2012.

Following is the link. This is direct Microsoft link so you can trust this application.

https://www.microsoft.com/web/handlers/webpi.ashx/getinstaller/WebNode11Pack.appids

https://www.microsoft.com/web/handlers/webpi.ashx/getinstaller/WebNode11Pack.appids

MVC: CS1061: System.Web.Mvc.HtmlHelper does not contain a definition for 'EnumDropDownListFor'

Error:
CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'EnumDropDownListFor' and no extension method 'EnumDropDownListFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Solution: to resolve this error, update MVC to latest version for my case, it is Microsoft ASP.Net MVC 5.1. Install it using NuGet package.
Search for Microsoft ASP.Net MVC.

Accept License. Read it or leave it, but accept it.. :P


Restart visual studio after installation of package.


This should resolve your error.

Friday, February 6, 2015

C# "Row not found or changed” Exception in LINQ to SQL (DBML)

Today I faced this issue with one of my project, which was created using LINQ to SQL. obviously it was having a .dbml file. Today I updated a field in database and created same entry in respective entity in dbml file. after that I started facing this issue. And when I clicked on newly added field and checked its property, I found its server data type was same but in different case(it was in all small letters). I rectified it from copying from other field and it solved my error.


Wednesday, February 4, 2015

Enum: Convert int value to string text.

Hi Following is the sample code to convert integer Enum value to string counterpart:

public enum Gender
{
    [Display(Name = @"No Preference")]
    NoPreference = 0,
    [Display(Name = @"Male")]
    Male = 1,
    [Display(Name = @"Female")]
    Female = 2
}

Use following code to get string text of enum:

int value_from_database = 1;
string UserGender = ((Gender)value_from_database).ToString();