Tuesday, 19 May 2009

Make ‘Folders View’ the Windows Explorer Default in Windows XP

Here is a link to a very handy blog post on how to make folders view in the explorer bar, in windows explorer default to folders view, instead of that silly blue common tasks bar.

http://www.daleisphere.com/make-folders-view-the-windows-explorer-default-in-windows-xp/

Great!

Thursday, 7 May 2009

How to create your own Code Signing Certificates

This has been a real pain for me recently. At work we had previously got code signing certificates from a certificate authority called Thawte. But when it came time to renew, they have been totally unhelpful, slow and inept. We have spent a month dealing with them trying to get a renewed certificated with no joy, so they can now go shove a banana because we are going to sort out our own certificates.

There is a great step by step guide in this forum discussion on channel9 that will help you set up your own code signing certificate.

http://channel9.msdn.com/forums/TechOff/13583-Generating-my-own-Software-Publishing-Certificate/

It's a fiddly process but worth it if all you need is a certificate to sign your code for use within your own network.

Wednesday, 29 April 2009

Using SyntaxHighlighter to display code snippets

Today I found a very handy javascript to display code snippets in web pages, and it can be adapted to work in Blogs as well.

The SyntaxHighlighter is very clever, and can display a lot of different code types, for example C#.


//Here is some C# Code
public void Method()
{
string test = "display some code in my blog";
test = test.tostring() + "and format it nicely";
}


There is a nice blog post by Arun K Viswanathan on how to easily adapt the code to work in Blogger. Check it out.

Thursday, 22 January 2009

Drop Constraint in MS SQL that has an unknown name

Now when I say unknown name I catually mean I know constraint I want to drop, however the name of the constraint was automatically generated by SQL Server and that name contains a random number e.g.

"DF__user__15502E78"


Well I could just write a script like this to get rid of it:


alter table [user]
drop constraint [DF__user__15502E78]


Fine that works, but what if I have created multiple version of that DB on different servers, each time SQL Server creates that constraint for me automatically it will contain a different random number in the name. So now when I run the script I worte above on a different server it fails.

So I wrote this little script below, that will search out the constraint and drop it for me. Now you will have to tweak this to get it to work for you as you may have more constraints as I ad in my DB, so you will need to alter the WHERE clause a bit to just get back the constraint name you are looking for.


-- Because this constraint is dyncamically created by SQL Server
-- the name of the constraint contains a random number
-- so the name will be different from DB to DB.
-- So the only way to drop the constraint is to use the script below.
DECLARE @constraint varchar(100)
SET @constraint = (SELECT OBJECT_NAME(OBJECT_ID) --AS NameofConstraint,
--SCHEMA_NAME(schema_id) AS SchemaName,
--OBJECT_NAME(parent_object_id) AS TableName,
--type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
AND OBJECT_NAME(parent_object_id) = 'user'
AND type_desc <> 'PRIMARY_KEY_CONSTRAINT')

print @constraint

declare @sqlscript varchar(200)

set @sqlscript = 'alter table [user] drop constraint ' + @constraint

print @sqlscript

exec(@sqlscript)


If you look at the select clause in the script above that is what i'm using to return the name of the constraint, you can uncomment the values in that select statement and just run and tweak that to get it just right for you and then insert it back into the rest of the script and your all sorted.

Friday, 28 November 2008

How to display HTML code in your Blogger Post

Convert the code to display in a blog post using this tool:

http://www.accessify.com/tools-and-wizards/developer-tools/quick-escape/default.php

Sorting and Paging a GridView with Custom DataSource in ASP.Net

So it's pretty simple to use a GridView in ASP.Net you can bind it to all sorts of data, but what I like doing is binding it to collections of my own custom objects. But this causes issues, because then you can't use all the nice in built paging and sorting without extra cosing. If you use the GridView wizard are hook it up to a SQLdatasource directly or a data set then all the paging and sorting is handled for you. But what if like me you want to use your own custome objects.

Well there is a way, it does require a little extras coding but it's nothing too complicated. Firstly lets look at the code to set up a standard GridView:

Client Side Code:



<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" onrowdatabound="GridView1_RowDataBound" onselectedindexchanging="GridView1_SelectedIndexChanging">

<columns>

<asp:boundfield datafield="ProjectID" headertext="ProjectID"/>

<asp:boundfield datafield="ProjectName" headertext="ProjectName"/>

<asp:boundfield datafield="Sector" headertext="Sector"/>

<asp:boundfield datafield="ProjectStatus" headertext="Project Status"/>

</columns>

</asp:gridview>


Server Side Code:


IList<project> projectList = Project.GetAllProjects();
GridView1. DataSource = projectList;
GridView1.DataBind();

Project Class:

public class Project
{

public Project()
{
}

public int ProjectID { get;set;}

public string ProjectName {get;set;}

public string Sector {get;set;}

public string ProjectStatus {get;set;}

static public Project GetAllProjects()
}





So this will display my list of custom objects in a simple GridView but I can't do paging or sorting, bummer. So what do we have to change to get it to work?

Well first on the ASP.Net page we add an ObjectDataSource control and point that to our datasource. This will help make our custome data collection more compatible with the GridView. We can then point out GridView at this ObjectDataSource.

We can then set paging on the GridView and Sorting. But for the sorting to work we still need some extra code. We need to make our custom data class comparable. So in the GetAllProjects method we add a parameter that will handle the sortExpression, we add this into the code of our GridView as well.

Then we add a switch statement in here to handle this sort expression. This requires us also to create a comparer class, we will call it ProjectComparer and in there we place code to handle the comparisons for each project value.


Client Side Code:

<asp:ObjectDataSource ID="dsProjects" runat="server" DataObjectTypeName="Business.DomainLogic.Project" TypeName="Business.DomainLogic.Project" SelectMethod="GetAllProjects" SortParameterName="sortExpression"></asp:ObjectDataSource>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound" DataSourceID="dsProjects"
AllowPaging="True" PageSize="8" AllowSorting="True"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="ProjectID" HeaderText="ProjectID" />
<asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName" />
<asp:BoundField DataField="Sector" HeaderText="Sector" SortExpression="Sector" />
<asp:BoundField DataField="ProjectStatus" HeaderText="Project Status" SortExpression="ProjectStatus" />
</Columns>
</asp:GridView>


Server Side Code:


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}

Project Class:

public List<project> GetAllProjects(string sortExpression)
{
List<project> projects = new List<project>();

switch (sortExpression)
{
case "ProjectName":
case "ProjectName ASC":
projects.Sort(ProjectComparer.CompareByName);
break;

case "ProjectName DESC":
projects.Sort(ProjectComparer.CompareByNameDesc);
break;

case "Sector":
case "Sector ASC":
projects.Sort(ProjectComparer.CompareBySector);
break;

case "Sector DESC":
projects.Sort(ProjectComparer.CompareBySectorDesc);
break;

case "ProjectStatus":
case "ProjectStatus ASC":
projects.Sort(ProjectComparer.CompareByProjectStatus);
break;

case "ProjectStatus DESC":
projects.Sort(ProjectComparer.CompareByProjectStatusDesc);
break;
}
}

ProjectComparer Class:

public class ProjectComparer
{
//COMPARE BY NAME

private static IComparer<project> _compareByName = new _sortName(false);
public static IComparer<project> CompareByName { get { return _compareByName; } }

private static IComparer<project> _compareByNameDesc = new _sortName(true);
public static IComparer<project> CompareByNameDesc { get { return _compareByNameDesc; } }

private class _sortName : IComparer<project>
{
bool _reverse;
public _sortName(bool reverse)
{
this._reverse = reverse;
}

public int Compare(Project x, Project y)
{
if (_reverse) return y.ProjectName.CompareTo(x.ProjectName);
else return x.ProjectName.CompareTo(y.ProjectName);
}
}


//COMPARE BY SECTOR

private static IComparer<project> _compareBySector = new _sortSector(false);
public static IComparer<project> CompareBySector { get { return _compareBySector; } }

private static IComparer<project> _compareBySectorDesc = new _sortSector(true);
public static IComparer<project> CompareBySectorDesc { get { return _compareBySectorDesc; } }

private class _sortSector : IComparer<project>
{
bool _reverse;
public _sortSector(bool reverse)
{
this._reverse = reverse;
}

public int Compare(Project x, Project y)
{
if (_reverse) return y.Sector.CompareTo(x.Sector);
else return x.Sector.CompareTo(y.Sector);
}
}


//COMPARE BY Project Status

private static IComparer<project> _compareByProjectStatus = new _sortProjectStatus(false);
public static IComparer<project> CompareByProjectStatus { get { return _compareByProjectStatus; } }

private static IComparer<project> _compareByProjectStatusDesc = new _sortProjectStatus(true);
public static IComparer<project> CompareByProjectStatusDesc { get { return _compareByProjectStatusDesc; } }

private class _sortProjectStatus : IComparer<project>
{
bool _reverse;
public _sortProjectStatus(bool reverse)
{
this._reverse = reverse;
}

public int Compare(Project x, Project y)
{
if (_reverse) return y.ProjectStatus.CompareTo(x.ProjectStatus);
else return x.ProjectStatus.CompareTo(y.ProjectStatus);
}
}

}




And that will give you paging and sorting on your GridView bound to a collection of custom objects. If i have missed any thing or it doesn't make any sense, plese just drop me a comment.