Category Archives: C#

A potentially dangerous Request.Form value was detected from the client – error in MVC4 in vb.net

In your Create Actions in a Controller in MVC4, if you try to save text with html tags then you get this annoying error.

A potentially dangerous Request.Form value was detected from the client

Description: ASP.NET has detected data in the request that is potentially dangerous because it might include HTML markup or script. The data might represent an attempt to compromise the security of your application, such as a cross-site scripting attack. If this type of input is appropriate in your application, you can include code in a web page to explicitly allow it. For more information, see http://go.microsoft.com/fwlink/?LinkID=212874.

This is a great protection if you do not want your client to save html text.

Here is how to get rid of this error

In the following example we have a create action which saves a new record. MVC4 controller will have two functions.

The first function is for GET ( this is the one which will render the page to save the new record

 

The second function is for POST. This function is responsible to receive the model
which has been populated by the data which the user has filled in on the form. We have to add the following text

<ValidateInput(False)>

 

for C# add

[ValidateInput(false)]

 

 

 

 

‘ GET: /admin/ManageAds/Create

Function Create() As ActionResult
Return View()
End Function

 

 

‘ POST: /admin/ManageAds/Create
<HttpPost()> _
<ValidateAntiForgeryToken()> _
<ValidateInput(False)>
Function Create(ByVal ad As Ad) As ActionResult
If ModelState.IsValid Then
db.Ads.Add(ad)
db.SaveChanges()
Return RedirectToAction(“Index”)
End If

Return View(ad)
End Function

 

How to duplicate a winform in Visual Studio easily

You start with a new project and you have created a winform with graphics and the functionality which you want to use in all your other forms in the same project.

One would think that it is easily done as we can simply copy and paste the existing form and rename it and then suddenly all will be well.

That is not the case when you Control C and Control V to copy the form. You will try to rename it but you might gets lots of errors. I have struggled with this may times.

I have discovered a fool proof way which works well.

There is an Export Functionality in Visual Studio which is handy. This functionality allows you to create a template and resuse the template form in your project .

Step 1

Go to File> Export Template in Visual Studio

Step 1
Step 1

 

Step 2

Step 2
Step 2

Select ITEM Template, You can also choose Project Template ( which will appear only in  your existing project).

where as ITEM template would be universally available for all new  or existing projects.

Step 3

 

Select the form which you want to clone

Step 3
Step 3

 

Step 4

 

You might need to select the references which you want to be included, select the ones which you think will need. You can always add them later using the code in your project  using the USING command  in C# or IMPORTS command in VB.NET after you have cloned a form.

Step 4
Step 4

 

Step 5

 

Provide a new name and some useful description to your new template.

Step 5
Step 5

Step 6

 

when you click on Finish in Step 5 the new template will be created in the Windows Folder as you can see below.

Step 6
Step 6

 

Step 7

 

When you click on ADD NEW ITEM in your Visual Studio project you should be able to see you newly created Template. You can use this template whenever there is a need . I found this approach simpler and safer than merely copying and pasting an existing form.

Step 7
Step 7

How to restrict free Data Entry in ComboBox in Visual Studio

When you create a Combo Box in Visual Studio  , the default behaviour is that it will show the list and it will also allow the data entry. So a user can type something in instead of selecting the options listed in the Combo Box. This can cause issues as you might want the user to only select the valid options which are listed in your combo box.

 

Here is a simple way to achieve this behavior. Highlight the Combo Box on your form. The properties window will be show.

Change the Drop Down Style to  DropDownList as shown below.

Your combo box will not allow the users to type in any free text. they will have to select the options listed in your combo box.

 

comboBox1

How to add Nuget Packages

How to Add Packages using NUGET in Visual Studio

Step 1

In Visual Studio Go to Tools > Library Package Manager > Manage Nuget Packages for Solution.

 

How to add Nuget Packages
How to add Nuget Packages

 

Step 2

You would get the following screen, you can check the packages which you have already available in your solution, you can search for new packages and install them to your solution.

Remember:  These packages need to be downloaded , each time you create a new Visual Studio solution

 

Packages in NUGET
Packages in NUGET

How to fix : Ajax.BeginForm doing full postback instead of partial.

I realised that in my MVC5 project  in Visual Studio I could not get the Ajax.BeginForm to work properly. It will always do a full postback.

 

I was trying to complete a Video Rating Page, where there are buttons to rate a video and once the user presses one of the rating button, it should thank the user and display the new rating value. I wanted this to shown on the same page without reloading the page, So I considered using AJAX.BeginForm which will simply work fine.

 

In order to have this AJAX functionality you will have to download the AJAX from NUGET.

 

Step 1

Install the following two highlighted packages from NUGET. Click here to check how to load Packages from NUGET in Visual Studio

 

Nuget

 

Step 2

Once you have installed the above two packages through NUGET then do the following.

Go to the _Layout file in your _shared  folder in the Views. ( Note that I am using VB.NET but the example will work for C# also.

 

_Layout in the Shared Folder
_Layout in the Shared Folder

 

Open the _Layout.vbhtml file and browse to the bottom.

 

You will see the following two lines.

@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)

 

Now add the following line after the above two lines.
<script src=”~/Scripts/jquery.unobtrusive-ajax.min.js”></script>

 

I made a mistake of adding this script at the top of my page and my AJAX calls refused to work.

Then I realised that the AJAX has to be loaded after loading the jquery. So I added it at the last.

 

 

 

 

AjaxCallsinMVC5
Add the script at the exact position show.

 

 

Step 3

Run your application.

Your AJAX Calls should work fine.

 

 

 

 

 

 

Could not load file or assembly ‘Antlr3.Runtime’ Error in Visual Studio 2013 Express

While working on the Visual Studio 2013 Express, I was happily burning the midnight oil to produce a new application, the application was coming on nicely.

But When I started work next day, suddenly I started getting the following error message.


Could not load file or assembly 'Antlr3.Runtime' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

After searching for long and trying different optionx the following solution did work for me and I was back in action. Hope you can benefit it from as well.

Step 1
Go to the file explorer and search for %Temp% (See the screen below)

Antlr3RuntimeIssueScreen1

Step 2
You would see lots of file in Temp folder. Delete them all.

Antlr3RuntimeIssueScreen2

Step 3

Run Visual Studio 2013 Express again and you would be able to work again on your project.

How to convert the Month Number to Month Name in C#

The following code will help if you have a need to convert the month number such as 3 to the month name like March.

 

//the following line of code will convert the month number to month name
string sMonthName = DateTime.Today.ToString(“MMM”);

// the following line of code will give you the Month Name and the Year
this.btnOne.Content = sFirstMonth + “-” + DateTime.Today.Year.ToString();

 

See the video below to see the working of the code live.