Sunday 30 March 2008

Windows Live Style WPF Gradient Fade

I have been playing with different gradients for an application that I am writing a wpf front end for.

After a lot of experimenting, I found the two gradients below look pretty awesome.

This first one creates a metalic or reflective style effect similar to the title bar on Windows Live, the border itself has rounded corners at the top giving it a tab header like quality.

<LinearGradientBrush MappingMode="RelativeToBoundingBox"
StartPoint="0,0"
EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0" Color="#7eb2d2" />
<GradientStop Offset="0.1" Color="#7eb2d2" />
<GradientStop Offset="0.11" Color="#7aafd1" />
<GradientStop Offset=".35" Color="#5599c4" />
<GradientStop Offset=".49" Color="#3586b9" />
<GradientStop Offset=".5" Color="#005c96" />
<GradientStop Offset=".65" Color="#005c96" />
<GradientStop Offset=".75" Color="#166a9f" />
<GradientStop Offset=".99" Color="#6aa4c5" />
<GradientStop Offset="1" Color="#7eb2d2" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>


This next one creates a nice relatively light blue shadow at the top of the area, allowing you to remove the total whiteness of an area without distracting from the fields etc on it...

<LinearGradientBrush MappingMode="RelativeToBoundingBox"
StartPoint="0,0"
EndPoint="0,1"/>
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0" Color="White" />
<GradientStop Offset="0.001" Color="White" />
<GradientStop Offset="0.0011" Color="#72a3d6" />
<GradientStop Offset="0.0125" Color="#aac7e5" />
<GradientStop Offset="0.025" Color="#d3e5f7" />
<GradientStop Offset="0.04" Color="#eaf3fd" />
<GradientStop Offset="1" Color="White" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>

Installing SQL Server Express With Your Application

There are a couple of ways to distribute SQL Server Express with your application.

The first and easiest way is to select it as a prerequisite in the Installer project's properties dialogue. Although this is the easiest way to deploy SQL Server Express with your application, it does require an internet connection on the client because it will download and install SQL Server Express from MS, or your own url if you want to state one....

http://www.programmers-corner.com/forums/post-15324.html

The other way is to embed the SQL Server Express installer into your application. Microsoft have moved the article about how to do this (try following the link to it from this article and you will get the generic "Article could not be found" page from MSDN)....

http://msdn2.microsoft.com/en-us/library/ms165639.aspx

I finally found the article at the following URL.... I haven't tried to follow this yet, but plan to use this method for distributing SQL Server Express with a new wpf application I am writing to calculate corporation tax....

http://msdn2.microsoft.com/en-us/library/bb264562.aspx

.NET Application Auto Update

I am currently looking to implement autoupdate functionality into a wpf .net application. There are 2 things I am looking into...

Firsthly, the following is free from Microsoft and you can use it easily to enable you to deploy autoupdates for your application...

http://msdn2.microsoft.com/en-us/library/ms978574.aspx

And, in case like me you don't have enough time to really look into how to use that patterns and practices and want to get a proof of concept prototype of your application auto updater up and running that you can polish closer to release, check out this blog post that provides really easy steps on how to get the autoupdater up and running...

http://codebetter.com/blogs/brendan.tompkins/archive/2004/06/10/16118.aspx

But, I found this required other patterns and practices to be installed before you can use this one.

Secondly, and seems the simplest one, I also found this from Microsoft that looks similar and a lot easier to set up...

http://windowsclient.net/articles//appupdater.aspx

One thing I did consider before looking into an auto updater was creating a Click Once deployment application, but decided against it because I want people without any internet connection to be able to install and use the application and Click Once would mean the user would have to have a connection at least for the first install. Also, it seems that the client would update the files and the database they connect to might not be updated etc... seems like it would work best with all clients connecting to a central database.

Sunday 16 March 2008

Redirect Subdomains with ASP.NET and C#

How to do sub domain redirection with asp.net c#

string strServerName = Request.ServerVariables["SERVER_NAME"].ToString().ToUpper();

if (strServerName.IndexOf("subdomain1.domain.com") > -1)
{
Response.Redirect("/subdomain1");
}
else if ....

Saturday 15 March 2008

Load SQL Server file at runtime without attaching the mdf file

There are situations where you want a quick and easy deployment for an application that runs from a SQL Server file and you don't want the user to have to have SQL Server client tools installed to attach to the mdf file etc. For instance if your app is a standalone desktop application where each user will use a db installed in the application folder.

In these scenarios, use the technique described in this article....

http://technet.microsoft.com/en-us/library/ms165716.aspx

It allows you to deploy an mdf file in the applications install folder and will load, or lift, that SQL Server database when the application runs without any need to attach the database .mdf file to an instance of SQL Server engine.

The user will need to have either SQL Server or the free SQL Server Express installed on their machine. Because SQL Server Express is free though and available as download from Microsoft this is usually not a problem.

One thing to consider with Vista though are the permission implications.....

http://blog.benhall.me.uk/2008/03/sql-server-and-vista-create-database.html

Friday 7 March 2008

WPF Binding Fails With Objects With Internal Properties

I created an executable Windows Presentation Foundation (WPF) assembly with a WPF window and a simple object with a couple of string properties to bind the form to.

In the window, I set the text property of a textbox to "{Binding Path=SomeValue}" where SomeValue was a property in my class called Something.

In the load event handler of the form I created a new instance of Something called something and set "DataContext = something;", which would set the data context of the window to the instance of Something.

This should have been all I needed to do to run the app and the window should open displaying the SomeValue property of something in the text box.

The app ran without any errors, no security or permission warnings or compile time errors or anything, but the text box was empty.

If I put a break on where I was setting the data context, I could see that SomeValue of something did have a value.

I eventually found out that, even though both the class and the WPF window are in the same assembly, the binding failed because the SomeValue property on Something was marked with an "internal" access modifier.

Changing the access modifier to "public" on that property got the application working, and the text box now displays the SomeValue property of something.

It did surprise me that there was no compilation or runtime error for this one, but there wasn't.

Wednesday 5 March 2008

Stock Icons

I have been looking at a number of stock icon websites. I needed to buy a good set of professional looking icons that covered all the icons I might want to display.

The best site I found by far was Glyphlab....

http://www.glyphlab.com/stock_icons/preview.html

They have a good range of icons in one set, so you don't have to buy a number of sets to get the icons you need. Also, they looked a lot more professional than most of the other companies I looked into and the icons come in all the sizes I might need.

You also get the photoshop files, so ifyou need to mix icons together (for instance to put a magnifying glass from one icon over a different icon for consistency, you have the files to do that)

The entire set cost me about £130 (UK Pounds Sterling), so they aren't the cheapest icon set, but it is cheaper than buying a number of different sets to get the whole range you need.

Monday 25 February 2008

Search Engine Optimisation (SEO)

I have been looking into Search Engine Optimisation (SEO) a little recently for my site and here are some great tips to get you going:

  1. Metadata: Get your metadata included and up to date including keywords, title, description, last updated etc. Google doesn't use keywords anymore because they have been abused by sites, but other search engines still do, but they do still use the title and description metadata.
  2. Get inbound links to your site: Get as many inbound links to your site as possible, there are a number of ways to do this including registering on free directories for your industry (search for what you are selling or providing along with terms such as "submit url", "add site" etc), do a similar search for guestbooks, forums etc in your industry and get links to your site on their as well (put your url in your signature for your forum/guestbook posts then you can get your link onto your posts without upsetting websites by blatant free advertising), the biggest win for links is on non forum/directory sites (i.e. other companies, organisations etc) but make sure people linking to your site contain relevant information or the Search Engines could take points off you for cheating.
  3. Put your titles in H1 tags: Google uses these more than keywords now, but don't put everything on your page in H1 tags, have 1 or 2 H1s and then put sub titles in H2, H3 etc or you could lose points for abusing the system.
  4. Use your images well: Don't have images on your site called things like 1lf45.jpg or even users.jpg, get your keywords into your image names, so if you are a company selling caravans in cornwall, don't call your logo logo.jpg, call it "caravans-cornwall-west-country-logo.jpg" if you have 50 images around your site you can get your keywords in there an extra 50 times! Also, remember the ALT tag on your images, again get your keywords in there and you can really improve your keyword density, sometimes by up to hundreds of times!!
  5. Place keywords wisely: Try to keep keyword heavy information in the top 25% and bottom 10% of your page. These are the areas focused on by search engines.
  6. Have a link to your sitemap on your homepage: Have a sitemap.xml page describing all the pages and their location in your site and have a link to it both at the top and bottom of your home page.
  7. Use free tools: Google provide free tools, such as their Analytics tool and also their Webmaster tool, which lets you register your sitemap with Google and see when Google last visited you and other information regarding any problems Google has with searching your site. Also, by using these tools, you are letting Google know you exist and declaring your allegiance to Google, which can't exactly lose you points with them.
  8. Submit your URL to Search Engines: Most search engines have a link to submit your site to them. Do this on as many search engines as you can, certainly all the main ones like Google, Yahoo, Alta Vista, AOL etc and repeat your submission every couple of months.
  9. Register on DMOZ: Register your site at http://www.dmoz.org/ which wins you favour at Google at the minute.
  10. Validate your pages: If you have time, it's worth validating your pages at http://www.w3c.org/ and getting their validated logos on your site. This could take time, but is a big win with Google.

All these things obviously take time, give yourself at least a couple of months to get through them all. Search Engine Optimisation is a long term process, sometimes you cannot tell for weeks or months if changes you make to your site have a positive or negative affect on your ranking in search engines.

Your hard work and patience will pay off in the long term though.

Friday 8 February 2008

Visual Studio 2008 - Switch between flow layout and absolute positioning

This was not the most obvious thing to find in Visual Studio 2008.

Back in 2003 and 2005 there was a property on the page (or something similar) that you could just set to switch between flow layout and absolute positioning.

In Visual Studio 2008 though, you need to go to Tools --> Options.

In the tree view on the left, open HTML Designer --> CSS styling and the bottom check box should be "Change positioning to absolute for controls added using toolbox"...etc.

The default will be for flow layout so it will be unselected. To absolute position controls, check this box then drag controls onto the form. Uncheck it when you want to start adding controls in flow layout again.

Tuesday 5 February 2008

Silverlight Install

If you are trying to incorporate Silverlight into your site, have a look at this article by Scott Gutherie on optimizing the Silverlight install.

It shows you how to get that nice blue dialogue form window pop up asking you to install Silverlight and covers the actual install as well.....

http://weblogs.asp.net/scottgu/archive/2007/10/30/optimizing-the-silverlight-install-experience.aspx

Saturday 2 February 2008

Comparing .NET Date on Server to Clients Date Anywhere in the World

I had to write a web application that will be hosted on US server with the date/time set to EST with daylight saving.

The application serves UK web clients, set to GMT with daylight saving, and I had to write javascript that would send the client time to the server and for the server to be able to compare it with it's own time.

I thought this would be difficult, but found this way to do it using UTC.

A little difficult to do because the base of the UTC date/time is different with .NET going for a base of 0001 AD and Javascript being from 1970. Also ticks in .net are measured in nanoseconds and javascript is measured in milliseconds.

Here is the javascript code:

var date = new Date();
var utcTicks = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

That will get you the milliseconds in UTC (Universal Time) from 1970.

In .NET, C# here but you could do similar in VB.NET and will work for ASP.NET, WinForms etc...

DateTime now = DateTime.UtcNow;
DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
long ticks = (now - baseTime).Ticks / 10000;

Note you need to subtract the base time that javascript uses so the ticks start at 1970 and also divide by 10000, because the ticks will be in nano seconds and javascript are in milleseconds.

Sunday 27 January 2008

ASP.NET 2.0 Configure Forms Security For SQL Server Database

I needed to set up an ASP.NET 2.0 website to use forms security using the built in login controls etc and it had to use an existing SQL Server database because the hosting the site was going onto would only provide 1 SQL Server 2005 DB (and it didnt support MS Access either).,

The tutorial on this site was great to follow:

http://aspnet.4guysfromrolla.com/articles/120705-1.aspx

As an overview though, the steps I had to take were:


  1. Use aspnet_regsql.exe, a tool located in the installation folder of the framework, that had a simple 3 step wizard. Point it at an existing database and it will create all the necessary tables, stored procedures etc for you.
  2. Create a page called login.aspx and drag a login control onto it from the login section of the toolbox.
  3. Configure the web.config to use your own database (by default it will use a sql server express db which it creates in the app_data folder of the app. Set the login page to be the one created in step 2.... http://msdn2.microsoft.com/en-us/library/6e9y4s5t.aspx
  4. Set up at least one initial user for the website by going to Project --> Configure Web Site and entering a users details. I'm using Visual Studio 2008 targeting .net 2.0, but Visual Studio 2005 must have a similar menu command if not the same.
  5. Run the site and you should be directed to the login page created in step 2, if not check your web.config is correctly configured for forms authentication. After logging on you will be redirected to the form you were running.

Saturday 26 January 2008

Difficult to find if error

I just spent a little while resolving an error... and it turned out to be a stupid little syntax error.

I had written something similar to the following:

if(evaluate1evaluate2evaluate3&&evaluate4);
{
// Do Something
}

The error was that, even though I thought it shouldn't, the Do Something was always doing something.

I started looking into the evaluations in the if statement and trying to work out why they seemed to be evaluating to true when I thought they shouldn't be.....

Then I spotted the problem....

You might have spotted it straight away when you saw the statement above....

I had put a semi colon after the if statement, before the curly bracketed section I wanted to execute if the if evaluated to true.

Once I had seen it, it made perfect sense as to why the behaviour was happening and I could have kicked myself.

Definitely the first thing I will look for if I experience this behaviour again.

Friday 25 January 2008

Hello World!

Hello all,

I am a .net software developer from the North East of England. My background is primarily in enterprise C# Web applications but have also worked on VB.NET and VB6 Windows applications.

I have MCSD for .NET certification.