views:

20

answers:

1

Afternoon all.

A very simple one for you today from thicky Rich.

I have a label I want to display as a lovely number format i.e. {0:N0}

Now, this label text equates to a query string value.

How do I go about formatting a label's text from a query string value in one fell swoop?

I have tried this

lblTotalPurchQS.Text = String.Format("{0:N0}",Request.QueryString["totalpurchasequantity"].ToString());

but with little success.

Any ideas or pointers?

+1  A: 

Don't use ToString on the incoming qeury string parameter, but convert it to an int first:

lblTotalPurchQS.Text = String.Format("{0:N0}",
                        int.Parse(Request.QueryString["totalpurchasequantity"]));

Note:

The above is not safe code. First, the conversion may fail with a conversion exception. You should also be HTML escaping the output, in case of XSS.

This is better:

int totalPurchaseQuantity;
if(int.TryParse(Request.QueryString["totalpurchasequantity"], out totalPurchaseQuantity))
{
  lblTotalPurchQS.Text = Server.HtmlEncode(String.Format("{0:N0}", totalPurchaseQuantity);
}
Oded
Top stuff Oded - thanks for that and the advice re. conversion exceptions.
Ricardo Deano