Skip to main content

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

How to fix this weird project error with in a very simple way?

Posted in ASP.NET, Tutorials, IIS

I just had a weird issue with my project: I’ve hit Ctrl+F5 to run and the browser opens. So far, everything ok. When I was going to attach the process I got a BSOD.

As if that wasn’t weird enough I got the error below after restarting everything and running my project again:

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

Luckily someone had this issue before (thanks Cloud Developer!) and I was able to solve it easily. What is the solution? Just empty your %TEMP% folder. Here is how:

  1. Click the Start button and then Run (or press Win+R)
  2. Type: %TEMP% in the box
  3. Click the OK button (The directory for temporary files opens)
  4. Select all files (press CTRL+A)
  5. Press the DELETE key on your keyboard

You don’t need to delete all files, but just the right ones. In case it doesn’t work and you need to delete all files it’s recommended to close all programs (even those on the system tray – beside the clock) and repeat the steps above.

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

The best way to use DropDownListFor with ASP.NET MVC

Things don't need to be difficult and, of course, do everything you can to reuse your code

Posted in ASP.NET, C#, Code snippets, Tutorials

One of the most annoying things on ASP.NET MVC is to create a decent dropdown list, and Scott Allen already covered it here nicely. While I like his explanation I often see people facing different scenarios. What I will describe here is the way I use to create the dropdown lists for all my projects.

The problems

  1. Create a common place dropdowns will be placed, so we can maintain it easily;
  2. Get the data from somewhere;
  3. Put the minimum necessary logic into the Controller;
  4. Show it in the View.

The solution

First of all, we need a class. I will use a Model, as example, since this data can also come from the database.

This class has only 2 properties since it’s all you need to use with a DropDownListFor. But keep in mind that your class can have other properties.

Now, we need the code to generate the items for the DropDownListFor.

I have this code in a helper specially build for dropdown lists. In this way, as mentioned before, I can ease the maintenance of the project. And if you didn't notice, this code is concatenating with the DefaultItem property, which I show below.

This property will add the standard "-SELECT-" item at the first position in our dropdown list.

Also, you will see once you download the code that I am hard coding the items in the DropDownListHelper's constructor. In the real world I would load the data directly in the property.

Now that we have the class (Model) and the helper, we will go for the ViewModel. This is a pretty standard situation: I personally like to keep my Model exclusively for database while my ViewModel is the one going to create the forms. The reason is that I can customize the ViewModel for my needs, not needing to have a Model with a lot of properties using [NotMapped] on the top.

Here is what we need in the ActionMethod.

And here is the View.

Some might say it’s an unnecessary effort, and I would agree if we would use only one dropdown list. However, when we start building bigger systems which are going to use multiple dropdown lists, this approach comes in handy.

You can check the full code in my GitHub repository: https://github.com/davidsonsousa/DropDownList

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

Create a video gallery with modal using ASP.NET and Bootstrap

Learn how to create a video gallery using the Youtube API with ASP.NET and Bootstrap modal

Posted in ASP.NET, API, Youtube, Code snippets, Tutorials, JavaScript

We are in the 3rd article of the series about Youtube API. The articles are:

  1. Load a single predefined video
  2. Load a custom video with the Youtube API in ASP.NET
  3. Create a video gallery with modal using ASP.NET and Bootstrap (this article)

We saw in the previous how to load a custom video from the backend using ASP.NET. For this one we will use the same approach to create our gallery, plus Bootstrap for the modal.

The player

We will need to put the player we saw on the first article in the Bootstrap modal, as it’s shown below.

The Javascript

There are 2 scripts to be used. The first one is the general script, which I've shown in the previous articles and it didn't change. The second one is the script below, which will handle the video gallery.

The View

As you saw in the javascript above we are using only one modal. The magic happens in the code below, where I am passing all parameters via data-* attribute. I am using the same Model from the previous article.

As you can see there is no mystery on working with Youtube API and ASP.NET. And the next article will show how you can detect the state of the videos, which is very important for tagging and user interaction.

Check the full code here: https://github.com/davidsonsousa/YoutubeAPI

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

Load a custom video with the Youtube API using ASP.NET

Learn how to load a video from the backend (ASP.NET) using the Youtube API

Posted in ASP.NET, API, Youtube, Code snippets, Tutorials, JavaScript

Let’s continue with this very simple series of articles about the Youtube API. The articles are:

  1. Load a single predefined video
  2. Load a custom video with the Youtube API in ASP.NET (this article)
  3. Create a video gallery with modal using ASP.NET and Bootstrap

On the previous article, we saw how to load a predefined video using the API. It was simple and clean, while we could load only that specific video. In the real world that would be a bit different since we would have to load the video from diferent sources, by using the backend. In this article we will see how we load the from the backend using ASP.NET

The player

We will use a div tag as player, which should be handled by the javascript later.

<div id="player"><div>

The Javascript

We will split this script in 2 parts: The general script, which makes the API calls and the Video script, which actually calls the video.

General script

We use this one just once, as we did before.

Video script

Here is the trick. Since I am using ASP.NET MVC here and I need to have some flexibility since we are using the back end to bring the video, I will need to put the script in a partial view. Also, I am passing the Video model, which should bring all the parameters needed for the video.

Video model

Here is the model I am using. Keep in mind that such model could bring the video data from a database, for example. Or some web service.

That's it.

Next article will be about a video gallery opening a video in a modal window. All of it using videos loaded from the backend in ASP.NET MVC and bootstrap.

Check the full code here: https://github.com/davidsonsousa/YoutubeAPI

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

Setting folder permissions using Web Deploy

Learn how to set folder permissions to your ASP.NET projects when using Web Deploy

Posted in ASP.NET, Web Deploy, Code snippets, Tutorials, Configuration

I was running into a very odd problem recently: I couldn't upload files to my website because of an error saying that I didn’t have permission to write files into the App_Data folder. I’ve decided to look into it and I realized that it began since I started using Web Deploy.

For those unfamiliar with Web Deploy I will tell you something: This is the best deployment tool for ASP.NET websites so far. It checks the files before you send to the server so it will send only those which had some change, publish entire databases and even pre-populate the tables.

The problem is that Web Deploy resets all folder permissions, which basically broke my file upload. And since ELMAH couldn't log the errors I spent some time thinking that my website was running flawlessly perfect. Silly me...

The solution I found was to create a .targets file in order to set the permissions during the deployment time. Let’s see how to do it:

  1. Create a .wpp.targets file in the root of your web project. You may not find this file in your File -> New window. In that case just create a normal Text File, change the extension and add XML content in it, as you can see in the gist below:
  2. Set the permissions to the folders you want. In my case, the App_Data was just enough but you can add other folders as well (lines 15 to 29).
  3. Deploy through Web Deploy. A very simple explanation of this step: When you deploy the MSBuild will run, pass through the .targets file you’ve created and set the permissions to the folders you’ve defined.

You might keep some things in mind:

  • Visual Studio caches the .targets files, which means that you will have to restart Visual Studio every time you want to make changes and see the effects when you are building the deployment package.
  • The package will not be rebuild unless you actually make changes in the .targets file.
  • Remember to always use Clean Solution in case you want to rebuild the deployment package.

For more information visit the Sayed Ibrahim Hashimi’s website.

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

Using web.config to make your ASP.NET project accept LESS CSS

Learn how to solve the 404 error you get when you try to run an ASP.NET project with .less files

Posted in ASP.NET, Tutorials, IIS, Configuration

Every ASP.NET developer must love the web.config file. Seriously, I will write about it some day. :-)

Some time ago, we have faced a very interesting problem in one of our ASP.NET projects. We have decided to use LESS CSS in order to optimize the front-end development and speed up the things a bit. While we knew that we should compile the LESS files before throw it to production we were facing an error during the development phase:

HTTP Error 404.3 - Not Found

The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

The reason: IIS doesn’t support .less files by default.

By this time we have 2 solutions: The first one is to call the responsible for the webserver and ask him to add a new MIME type into the IIS. But thanks to the .NET architecture we don't need to do that. All we need to do is to add some extra lines to your web.config, inside , as seen bellow:

<system.webServer>
  <staticContent>
    <remove fileExtension=".less" />
    <mimeMap fileExtension=".less" mimeType="text/css" />
  staticContent>
system.webServer>

As you saw it’s simple, clear and useful. Keep in mind that you can use the same method to solve the problem with other non-supported files such as webfonts (.eot, .woof), videos (.mp4, .ogg, .m4v), images (.svg, .svgx) and so on (as Mads Kristensen wrote).

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

How to upload a file using ASP.NET MVC and AJAX

A very simple file uploader using the progress bar component from Bootstrap

Posted in ASP.NET, C#, Tutorials, MVC, AJAX

I've decided to get my old upload post and upgrade the components I am using. For example: Before it was an MVC3 project made on Visual Studio 2010 using the old version of everything. Now it's a Visual Studio 2012 project, ASP.NET MVC4, using bootstrap and the newest version of each plugin I've used before.

First step

First of all we need to create a Model just like Scott Hanselman made in his article. Then we will have the name, length and type of our file:

public class UploadFilesResult
{
	public string Name { getset; }
	public int Length { getset; }
	public string Type { getset; }
}

Second step

We will need to create an Action Method which will take care of the upload. It will be called by using the http POST method, get the list of selected files and save into App_Data. This Action Method should return a json informing the name, length and type of the files:

[HttpPost]
public ContentResult UploadFiles()
{
	var r = new List<ViewDataUploadFilesResult>();
 
	foreach (string file in Request.Files)
	{
		HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
		if (hpf.ContentLength == 0)
			continue;
 
		string savedFileName = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileName(hpf.FileName));
		hpf.SaveAs(savedFileName); // Save the file
 
		r.Add(new ViewDataUploadFilesResult()
		{
			Name = hpf.FileName,
			Length = hpf.ContentLength,
			Type = hpf.ContentType
		});
	}
	// Returns json
	return Content("{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}""application/json");
}

Third step

After that we only need to download a jQuery plugin called jQuery File Upload. It brings us lots of cool stuff but, for the sake of simplicity, we are gonna use only 2 files (apart from the others I am already using): jquery.fileupload.css and jquery.fileupload.js:

Fourth step

We need now a way to send these files. This is what I've created - just for the upload - but you can adapt into your page:

<div class="container">
	<span class="btn btn-success fileinput-button">
		<i class="glyphicon glyphicon-plus">i>
		<span>Add files...span>
		<input id="fileupload" type="file" name="files[]" multiple>
	span>
	<br />
	<div class="progress">
		<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width0%;">
			<span class="sr-only">0% completespan>
		div>
	div>
	<br />
	<div class="file_name">div>
	<br />
	<div class="file_type">div>
	<br />
	<div class="file_size">div>
div>

Notice 4 things:

  1. I didn't use HtmlHelper to create the form. The reason is the ease to create the form without it. Also, we don't need to use HtmlHelpers all the time;
  2. I am not using a form tag. The plugin takes care of everything;
  3. The progress bar, thanks to bootstrap;
  4. The divs at the end, which will only receive the json data.

Last step

Now we need to make the javascript in order to make this work. I am using the jQuery File Upload plugin calling the Action Method UploadFiles created in the HomeController. In addition I have also attached an event which shows the upload status to the user by using the bootstrap progressbar component:

<script type="text/javascript">
	$(document).ready(function () {
		$('#fileupload').fileupload({
			dataType: 'json',
			url: '/Home/UploadFiles',
			autoUpload: true,
			done: function (e, data) {
				$('.file_name').html(data.result.name);
				$('.file_type').html(data.result.type);
				$('.file_size').html(data.result.size);
			}
		}).on('fileuploadprogressall'function (e, data) {
			var progress = parseInt(data.loaded / data.total * 100, 10);
			$('.progress .progress-bar').css('width', progress + '%');
		});
	});
script>

That's it. Now you are ready to add this feature to your website, without any postback.

Download the code here: jQueryFileUploadMVC4.zip (5.84 MB). Yes, almost 6MB thanks to the packages.

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

How to fix "Error in processing. The server response was: Greylisted, please try again in X seconds"

Yes! A solution for this strange error! How many times have you seen this one while trying to send an e-mail?

Posted in ASP.NET, C#, Code snippets, Tutorials

I had a quite interesting Friday. It was the last day of a class in which I had to replace the trainer. They had some problems with him and I was the choosen one for the replacement. Everything went well so far. After that I went to check the error log of my blog (I use Elmah, by the way) and I found an issue I've never seen before:

System.Net.Mail.SmtpException
Error in processing. The server response was: Greylisted, please try again in 240 seconds

The time may vary. But after google it for a while I found it! My server was implementing Greylisting which, according to Wikipedia, is a method of defending e-mail users against spam. So, the non-recognized e-mails are automatically rejected for a while.

And how to fix that?

All I needed to do was to create a mail account on my server and add it to my web.config in the following way:

<system.net>
	<mailSettings>
		<smtp>
			<network defaultCredentials="false" host="Mail@DomainName.com" port="25" userName="username@DomainName.com" password="ThisIsAPassword"/>
		smtp>
	mailSettings>
system.net>

Needless to say, you need to put your own host, username and password. :-)

And that's it.

In time: The answer I found here.

	<system.net>
		<mailSettings>
			<smtp>
				<network defaultCredentials="false" host="mail.davidsonsousa.net" port="25" userName="contact@davidsonsousa.net" password="D#sousa12"/>
			smtp>
		mailSettings>
	system.net>

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network:

A quick reference for the ASP.NET inline server tags. Great for beginners!

Here is a complete guide of the ASP.NET inline tags. I am sure you will find it useful!

Posted in ASP.NET, Tutorials

I have to tell you: Sometimes I get confused on which server-side tag I should use to make my codes. Normal since I never use all of them so oftem. If you suffer from the same issue this text might be useful as a quick reference.

<% ... %>

The most common of the inline tags. It's used when you need to make some small code on the aspx page or render some expression there. For example:

<% if(isAdmin) { %>
Hello Admin!
<% } %>

<%= ... %>

This is the simplest way to display some information in an ASP.NET page. The equal sign acts as Response.Write rendering the object directly on the page. For example:

Today is <%= DateTime.Now %>

<%: %>

This was implemented in ASP.NET 4 and it's basically the same as above but with one small thing: This server tag automatically does the HTML Encoding for you. So, instead of using Server.HtmlEncode(htmlText) on your code you can simply use as the example:

Here is some html <%: htmlText %>

<%-- ... --%>

This tag is a server-side comment. Since this code is not rendered on the browser you can use it in case you don't want anyone to see the comments you leave on the system. Example:

<%-- Nobody is gonna see this --%>

<%# ... %>

If you want to do some data binding, you have to use this tag. It can be used with DataBinder.Eval(), DataBinder.Bind() or any member which you have in the page (public or protected). The only "problem" is that you have to use these tags only in server-side elements (with runat="server"). You will find this tag being used in some controls such as GridView, DetailsView and Repeater. Example:


    
         <%# Eval("LastName") %>, <%# Eval("FirstName") %>
    

 

<%$ %>

This one is used to evaluate expressions in a configuration file. For example:

    SelectCommand="SELECT * FROM [Employees]"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>">

<%@ %>

This tag is used to specify the settings used by the page and user controls. Example:

<%@ Page Language="VB" ContentType="text/xml" %>

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network: