Loading HTML fragments with jQuery

When I want to load a html fragment I prefer the contents of the container div in the page and the HTML fragment to have the same ID, like:

 

Master page

<!DOCTYPE html>
<html>
<head>
<title>Master page</title>
</head>
<body>
<!--Placeholder container. -->
<div id="page"></div>
</body>
</html>

Page (html fragment)

<!DOCTYPE html>
<html>
<head>
<title>Page (html fragment)</title>
</head>
<body>
<!-- Page (html fragment to load). -->
<div id="page"></div>
</body>
</html>

 

Then the following code can be used to replace the contents of the "placeholder" div, by the contents of the ”html fragment" div:

 

Code

// Loads a html fragment.
// It replaces the content of the given DOM element, with the html fragment contents.
// It expects both the current page and the html fragment to contain the same "id" as the given id.
//
// Parameters:
// [id] example ["#page"].
// [url] example ["Features/Login/LoginView.html"].
function loadHtmlFragment (id, url)
{
// Use jQuery "ajax" instead of jQuery "load", because "load" will get the whole div, 
// when used with selector "#page" and not the contents.
// You can get only the contents with "load", by using "url #page > *", 
// but that will only get the child elements of the div, 
// stripping out any direct child text.
$.ajax({
url: url,
type: 'GET',
dataType: "html",
success: function (data)
{
// Strip out any javascript by using "parseHtml".
// Only use the contents of the html fragment by using "html()".
var dom = $("<div>").append($.parseHTML(data)).find(id).html();
// Adjust UI.
$(id).html(dom);
}
});
};

Casting when dealing with nullable fields in a SqlDataReader

When dealing with nullable fields in a SqlDataReader, use:

int? field_a = reader["field_a"] as int?;
string field_b = reader["field_a"] as string;

instead of:

int? field_a = Convert.ToInt32(reader["field_a"]);
string field_b = Convert.ToString(reader["field_a"]);

http://stackoverflow.com/questions/2141575/how-to-efficiently-convert-cast-a-sqldatareader-field-to-its-corresponding

How to get the day of week of a all dates between start and end date in T-SQL

If you want to get a list of all dates between a start and end date and their corresponding day of week in T-SQL, you can use the following code:

set datefirst 1
declare @StartDate date = '2014-01-20'
declare @EndDate date = '2014-01-27'
;with cte (dt, dw) as
(
select cast(@StartDate as date), datepart(dw, @StartDate)
union all
select dateadd(day, 1, dt), datepart(dw, dateadd(day, 1, dt))
from cte
where dateadd(day, 1, dt) <= @EndDate
)
select dt, dw
from cte

Result

image

Fixing: User ‘Domain\Admin’ does not have required permissions. Verify that sufficient permissions have been granted and Windows User Account Control (UAC) restrictions have been addressed. On a Windows 8.1 machine with SQL Server 2012 installed.

 

After I installed Microsoft SQL Server Reporting Services 2012 and configured it with the Reporting Services Configuration Manager and clicked on de Report Manager URL:

image

 

I got the error message:

image

 

User ‘Domain\Admin’ does not have required permissions. Verify that sufficient permissions have been granted and Windows User Account Control (UAC) restrictions have been addressed.

 

Solution

Turns out you have to run your browser (e.g. Internet Explorer) as an Administrator

 

image

 

The same is true for the Web Service URL:

 

Error

The permissions granted to user ‘Domain\Admin’ are insufficient for performing this operation. (rsAccessDenied).

image

 

Run as administrator:

image

How to validate a value can be cast to an integer in T-SQL

Validating if a value is an integer can be done by using TRY_CAST in SQL Server 2012 or a combination of ISNUMERIC and cast to float in <= SQL Server 2008.

declare @value as varchar(255) = '1.6'
-- SQL Server 2008
select case        when isnumeric(@value) = 1
then 'Cast succeeded, value is: ' + cast(cast(cast(@value as float) as int) as varchar(255))
else 'Cast failed.'
end as    Result
-- SQL Server 2012
select    case    when try_cast(@value as float) is not null 
then 'Cast succeeded, value is: ' + cast(cast(cast(@value as float) as int) as varchar(255))
else 'Cast failed.'
end as    Result

image

 

If you use try_cast(@value as int) the cast will fail on a float ‘1.6’, like:

 

image

Building in the cloud and deploying locally with visualstudio.com with Microsoft Visual Studio 2013

If you use the Team Foundation Service (on visualstudio.com) as your source control system, you can build your sources in the cloud, but access the build outputs locally.

Just start a new project on visualstudio.com and add a solution with some projects to it.

In my case a ASP .NET Web Api 2.0 project, a integration test project and a unit test project:

clip_image001

In the source control explorer it look like:

clip_image002

Create a build definition

Create a build definition in Microsoft Visual Studio 2013

If you choose for the option "Copy build output to the following Source Control folder (this folder will be created if it does not exist)".

The output of the build will be put in TFS.

So after the build you can download it from TFS and deploy locally.

Downloading can be automated by using PowerShell or some other tooling.

clip_image003

When you choose to put the output of the builds in TFS, you will find a "Drops" folder in the source control explorer.

clip_image004

clip_image005

The build result summary, shows some statistics on the build:

clip_image006

 

The builds can also be executed online and the result can also be seen online in the browser:

image

Just a reminder: null = null, is not true in T-SQL

If you have a nullable column "Description" in one table and a nullable column "Description" in an other table and you join both tables on this column, records with both null in the field "Description" will not match.

if object_id('[dbo].[Product1]') is null
begin
create table [dbo].[Product1]
(
Id int not null identity(1,1) constraint PK_Product1_Id primary key,
[Description] varchar(max) null
)
end
go
if object_id('[dbo].[Product2]') is null
begin
create table [dbo].[Product2]
(
Id int not null identity(1,1) constraint PK_Product2_Id primary key,
[Description] varchar(max) null
)
end
go
insert into Product1 values (null)
insert into Product1 values ('')
insert into Product1 values ('gevuld 1')
insert into Product2 values (null)
insert into Product2 values ('')
insert into Product2 values ('gevuld 1')
select        p1.*, p2.*
from        Product1 p1
inner join    Product2 p2 on p1.[Description] = p2.[Description]

Results in

image

 

and you might have expected

image

 

Solution is to use isnull function, like:

 

select        p1.*, p2.*
from        Product1 p1
inner join    Product2 p2 on isnull(p1.[Description], 'Not a value') = isnull(p2.[Description], 'Not a value')