How to setup Visual Studio to validate Html5 tags?

If you are developing Asp.Net or Asp.Mvc and would like to have warning or error if you are not using Html5 correctly, it’s plausible.

You have to go in Visual Studio 2010 into Tools>Options>Text Editor>HTML>Validation. From there you will be able to set up some configuration about how to display messages to you.

How to easily update JQuery library with ASP.MVC project

It’s always a good habit to have the latest framework updated. This way, you have most recent bugs fixed and all the new features available. This can be done manually by changing files but if you have NuGet installed can be done easily without having to download manually and to check for compatibility.

You can use Update-Package jquery to update JQuery which will not only get the latest JQuery version of the main library but also will get the UI, Validation and other Javascript librairies that is close to JQuery.

To be able to execute Update-Package jquery you will need launch the Package Manager Console. This is located in Tools>Library Package Manager>Package Manager Console.

The user instance login flag is not supported on this version of SQL Server

When importing an existing project that you have not setup you may have when executing a SqlConnection this kind of error : flag is not supported.

Error Message: The user instance login flag is not supported on this version of SQL Server.

This can occur when the previous developer had in his connection string a reference to “User Instance“. This need to be removed.

A user instance is like a normal database instance but it is created on demand. Normally, instances are created during the creation of the database.

To remove this error, you just need to remove this configuration of the Sql connection string.

Differences between ViewData, ViewBag, Session and TempData

These three mechanisms transport information from one page to the other. ViewData and View bag can be threat in the same group because they are both the same thing. The ViewBag is an object that use ViewData. The main difference is that it’s possible to access the values from dynamic properties instead of accessing from an array with a string key.

        ViewData["Name"] = "Patrick";
        //Or
        ViewBag.Name = "Patrick";

So, in fact, we are storing an object value into a dictionary which is defined to have Object type value and string key.

TempData is also a dictionary of string key and object value. The difference is that the life cycle of the object. TempData keep the information for the time of an HTTP Request. This mean only from one page to another. This also work with a 302/303 redirection because it’s in the same HTTP Request.

      TempData["Name"] = "Patrick"; 

The ViewData and ViewBag won’t have any value after a redirection. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call. The life cycle of the ViewData and ViewBag is shorter than TempData.

The last mechanism is the Session which work like the ViewData, like a Dictionary that take a string for key and object for value. This one is stored into the client Cookie and can be used for a much more long time. It also need more verification to never have any confidential information.

Insert data into Excel with C#

It’s possible with .Net to write data into Excel’s cells. This can be done by using the library of Microsoft.Office.

The first step is to add a reference into you project by right clicking on the References folder and add the COM of Microsoft Office Excel.

Here is a small example that show you how to open an Excel worksheet and add value to 2 cells. It will save the content back the to same file. The first approach do not use the Dynamic keyword which will be more verbose. The second example, do the same thing in less code by using Dynamic.

    _Application docExcel = new Microsoft.Office.Interop.Excel.Application();
    docExcel.Visible = false;
    docExcel.DisplayAlerts = false;

    _Workbook workbooksExcel = docExcel.Workbooks.Open(@"C:\test.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    _Worksheet worksheetExcel = (_Worksheet)workbooksExcel.ActiveSheet;

    ((Range)worksheetExcel.Cells["1", "A"]).Value2 = "aa";
    ((Range)worksheetExcel.Cells["1", "B"]).Value2 = "bb";

    workbooksExcel.Save();
    workbooksExcel.Close(false, Type.Missing, Type.Missing);
    docExcel.Application.DisplayAlerts = true;
    docExcel.Application.Quit();

Second example:

 _Application docExcel = new Application{Visible = false};

    dynamic workbooksExcel = docExcel.Workbooks.Open(@"C:\test.xlsx");
    var worksheetExcel = (_Worksheet)workbooksExcel.ActiveSheet;

    ((Range)worksheetExcel.Cells["1", "A"]).Value2 = "test1";
    ((Range)worksheetExcel.Cells["1", "B"]).Value2 = "test2";

    workbooksExcel.Save();
    workbooksExcel.Close(false);
    docExcel.Application.Quit();

How to do a hit test with Point and Shape?

In some situation, you may need to know if the mouse has hit a specific polygon drawn in a canvas. This can be easily done with Silverlight using the VisualTreeHelper.

First of all, you need to have a listener to the mouse even.

 void myCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
	e.Handled = true;
	Point currentMousePosition = e.GetPosition(this.myCanvas);
	var underMouseControls = GetControlFromCoordonate(currentMousePosition);
}

The second step is to create the method GetControlFromCoordonate to return a list of Control that has been clicked.

public IEnumerable<Control> GetControlFromCoordonate (Point coordonate)
{
	return VisualTreeHelper.FindElementsInHostCoordinates(coordonate, this.myCanvas).OfType<Control>();
}

This will return an enumeration of all UIElement which be cast to have only Control. You can even filter more if you search specific control.

So, the VisualTreeHelper may not have a HitTest method but with the FindElementsInHostCoordinates, you can filter the result to have the same result.