Thursday, 8 April 2010

HTML Redirect

I was asked the other day how to perform an HTML redirect and thought I'd share it. I know it's quite a simple thing, but as they say on Who Wants To Be A Millionaire, the question's only easy if you know the answer!




<title>Your Page Title</title>
<meta content="0;url=http://www.domain-name-to-redirect-to.com" http-equiv="REFRESH"></meta>

Your optional text here.


The first line that we're interested in is, <meta content="0;url=http://www.domain-name-to-redirect-to.com" http-equiv="REFRESH"></meta>.  This will redirect the user to the appropriate website after 0 seconds.  You can make the browser redirect as short or as long as you wish.

Finally, in the BODY tag, you can enter optional text which you can display to the user, informing them, perhaps, that you are redirecting them.
The HTML redirect has a further application; I frequently write websites for customers who sign up for hosting packages where the control panel does not allow the MIME types to be altered or added to. So if someone accidentally lands on www.website-name.com, your files will be displayed (.aspx, .aspx.cs...). If you really want them to go to www.website-name.com/Login.aspx, you can write a redirect page. First, find out from the hosting provider the default MIME types and then create a file, using one of the names provided, copy the above code, and you have your own redirect to www.website-name.com/Login.aspx!

Wednesday, 7 April 2010

Webservices and data structures

Today I needed to write some webservices to talk to our Microsoft Navision system, but instead of just passing over individual data types, I needed to pass over large amounts of data in one call. To achieve this, I created classes to hold the data to pass back and forth. To achieve this, I created a folder in my webservice, and placed the classes in the folder.
The structure of one of the classes (customer) is show below:
namespace WebService
{
  public class wss_Customer
 {
   public string No_ = string.Empty;
   public string Names = string.Empty;
   public string Name2 = string.Empty;
   public string Address = string.Empty;
   public string Address2 = string.Empty;
   public string City = string.Empty;
   public string Contact = string.Empty;
   public string PhoneNo_ = string.Empty;
   public decimal CreditLimit_LCY = 0.0M;
   public string CurrencyCode = string.Empty;
   public string SalespersonCode = string.Empty;
   public string CustomerDiscGroup = string.Empty;
   public bool PricesIncludingVAT = false;
   public string PostCode = string.Empty;
   public string County = string.Empty;
   public string VATBusPostingGroup = string.Empty;
   public int CreditStatus = 0;
   public int Blocked = 0;
   public bool AllowLineDisc_ = false;
   public string CustomerPriceGroup = string.Empty;
 }
}
Once I had created the classes, I thought I'd return to my console, calling app, update the webservice reference and start filling the class structure, without first writing the web method routines. But I couldn't see the class structures. After much 'faffing' about, I found that I needed to at least write the web method stubs, including the necessary class structure, as show below:
[WebMethod]
    public int PostUnsynchedCustomer_18(wss_Customer custToSynch, string value)
   {
   }
After doing this, compiling the webservice, updating the webservice reference in the console app, I found that I could see the class structure and start populating it with data.

I would love to hear if anyone else has had similar problems or alternative ways of achieving this.

Tuesday, 6 April 2010

Altering the error message in a asp:CustomerValidator


I'm currently using an asp:CustomerValidator, and depending on some of the logic within the method that I'm using, I wanted to alter the error message.  So after a little playing around I found that you can access the error message through the sender param that's passed in to your validation method.  So here's the method signature which everyone will be familiar with:

public void ETAValueCheck(object sender, ServerValidateEventArgs args)
So through the sender param you can access the ErrorMessage property as follows:
((CustomValidator)sender).ErrorMessage = "This is a test";
And that's it!  It really is that easy!  Simples! 


ASP.NET Caching

If you want to cache images or anything, in your ASP.NET page, you can either use the following in the markup:

<%@ OutputCache Location="None" %>


or set the Cache property of the HttpResponse in your code-behind class:


Response.Cache.SetCacheability(HttpCacheability.No Cache);

Monday, 5 April 2010

CSS Gradient Background

I wanted to create a gradient background to use on one of my web sites today, and thought I'd share this.
Step 1
Create a new graphic file in your favourite graphics package (Firefox, Photoshop etc).  Make the image the following dimensions:
Width: 1px
Height: 1000px
Now choose web safe colours, as this will ensure that the colours are displayed as you want them to.
Finally, using the gradient tool, fill the 1px x 1000px area with your chosen colours.
Step 2
Now choose where you want the background gradient to appear, i.e. the body tag, a container tag etc.
Open your CSS file and type the following:
.body
{
background-image: url(../Images/GradientBackground.jpg);
background-repeat: repeat-x;
}
There you have it, you're background will now have a lovely gradient colour!

Sunday, 4 April 2010

SQL Server Row_Number()

I wanted to do some, for me, unusual SELECT statements on data that we had for one of our customers; I wanted to select data between two sequential values, but unfortunately, none of the columns assisted me in anyway (they have a lot of varchar columns).  So I remember that with SQL Server 2005, Microsoft provided some sort of row ID.  So after a bit of digging around (in other words Googling!), I found Microsoft provide the function ROW_NUMBER().  Ha ha, just the ticket!  So after 10 minutes of playing with the new function, here is what I came up with:

WITH AS

(

SELECT ROW_NUMBER() OVER (ORDER BY ASC) AS 'ROWID',
* FROM

)

select * From where ROWID between 0 and 10

The isn't the normal #temp table, because if you do a select from it, the table doesn't exist, only within the WITH clause.
I hope this helps others as it took me time to find the answer and if anyone knows what the WITH clause means, I would be interested to hear.

Saturday, 3 April 2010

You are not authorized to view this page

I was at a customer site the other day, attempting to set up IIS web site for my web service (.asmx), but I kept getting the error message, "You are not authorized to view this page".  I couldn't figure out what was causing this and was trying everything from changing the permissions to deleting the web site and starting again, but nothing was working.
After a while I managed to get a different error message saying that the XML for the file, filename.asmx.cs, was incorrect!  What?  Then it dawned on me that I had written the web service against .Net 3.5, and so installed the .Net 3.5 re-distributable.  Great, the XML message disappeared, but the "...not authorized" page was back! Cry
So after a lot of playing around, I found that the following settings made the error message disappear:
1. Go to the Directory Security tab and put a tick in the box, Enable Anonymous Access, and un-tick the remaining four boxes.
2. Go to the web service extensions section and click the Allow button for, All Unknown ISAPI Extensions.
3. And I uninstalled the .Net 3.5 re-distributable and then re-install it.
Brilliant!  The XML message went away and so too did the error message. Cool
However, I then had a different message from my code saying that the user, NETWORK ACCESS, code couldn't access the Microsoft.Net x.x folder.  So I navigated to the said folder, and gave read & write permissions to the user, NETWORK ACCESS.
This finally solved my problem and I was away.  Hope this helps.