Archive for » September, 2008 «

Monday, September 29th, 2008 | Author: admin

Field

Expression

Description

Email

^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$

Validates an Email Address

URL

^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$

Validates a URL

Name

^[a-zA-Z''-'\s]{1,40}$

Validates a name. Allows up to 40 uppercase and lowercase characters and a few special characters that are common to some names. You can modify this list.

Password

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$

Validates a strong password. It must be between 8 and 10 characters, contain at least one digit and one alphabetic character, and must not contain special characters.

Non- negative integer

^\d+$

Validates that the field contains an integer greater than zero.

Currency (non- negative)

^\d+(\.\d\d)?$

Validates a positive currency amount. If there is a decimal point, it requires 2 numeric characters after the decimal point. For example, 3.00 is valid but 3.1 is not.

Currency (positive or negative) ^(-)?\d+(\.\d\d)?$ Validates for a positive or negative currency amount. If there is a decimal point, it requires 2 numeric characters after the decimal point.

Taken from MSDN article 998267

Thursday, September 25th, 2008 | Author: admin
//Declare Objects
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader r;

        StringBuilder SQL = new StringBuilder();
        SQL.Append(" SELECT DISTINCT(category)");
        SQL.Append(" FROM resources");
        SQL.Append(" ORDER BY category");

        string connectionString = ConfigurationManager.ConnectionStrings[
         "DefaultDatabase"].ConnectionString;

        //Initialize connection
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand(SQL.ToString(), conn);

        try
        {
            conn.Open();
            r = comm.ExecuteReader();
            //Bind the repeater to the data source
            rptParent.DataSource = r;
            rptParent.DataBind();
            //close the repeater
            r.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
        finally
        {
            conn.Close();
        }

Category: ASP.NET, C#  | Leave a Comment
Thursday, September 25th, 2008 | Author: admin

If you want to be able to add an item into your dropdown list from the code behind, do the following;

DDCategory.Items.Insert(0, "Select Category...");

 

Category: ASP.NET, C#  | Leave a Comment
Tuesday, September 23rd, 2008 | Author: admin
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDCategory();
        }
    }

    protected void BindDDCategory()
    {
        //declare the objects
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader r;

        //read the connection string from web.config
        string connectionString = ConfigurationManager.ConnectionStrings[
        "DefaultDatabase"].ConnectionString;

        conn = new SqlConnection(connectionString);

        StringBuilder SQL = new StringBuilder();
        SQL.Append(" SELECT DISTINCT(category)");
        SQL.Append(" FROM resources");
        SQL.Append(" ORDER BY category");

        comm = new SqlCommand(SQL.ToString(), conn);

        try
        {
            conn.Open();
            r = comm.ExecuteReader();

            //populate the dropdown list
            DDCategory.DataSource = r;
            DDCategory.DataValueField = "category";
            DDCategory.DataTextField = "category";
            DDCategory.DataBind();

            //close the reader
            r.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
        finally
        {
            //close the connection
            conn.Close();
        }

    }


Category: ASP.NET, C#  | Leave a Comment
Tuesday, September 16th, 2008 | Author: admin

Today I had a problem of trying to call a javascript file from my masterpage. I have a script that swaps around the page’s style sheets when a user clicks on a link to increase/decrease the font size. My problem was that the path to the file was wrong if I created a page in a directory lower than where the masterpage is stored.

My solution is to set the path in the PageLoad event in the code behind and call the script then.

Original Code (aspx page):

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Therapy Focus</title>
    <link href="~/css/default.css" rel="stylesheet" type="text/css" />
    <link rel="alternate stylesheet" type="text/css" href="~/css/default.css" title="default" />
    <link rel="alternate stylesheet" type="text/css" href="~/css/default2.css" title="default2" />
    <link rel="alternate stylesheet" type="text/css" href="~/css/default3.css" title="default3" />
    <script type="text/javascript" src="scripts/styleswitcher.js" ></script>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>

Solution (code behind):

protected void Page_Load(object sender, EventArgs e)
    {
        string jscriptPath = Request.ApplicationPath + "/scripts/styleswitcher.js";
        Page.ClientScript.RegisterClientScriptInclude(UniqueID, jscriptPath);
    }

Friday, September 12th, 2008 | Author: admin

I’m always forgetting how to format this, so I’m posting it here so I can find it easily;

<%#Eval("contentDate", "{0: dd MMM yyyy}").ToString() %>

 

This gives you 12 Sep 2008

 

Category: ASP.NET  | Leave a Comment
Friday, September 12th, 2008 | Author: admin

ToString("d/MM/yyyy") yields: 21/04/2003
ToString("d/MM/yy") yields: 21/04/03
ToString("d/M/yy") yields: 21/4/03
ToString("d/M/yyyy") yields: 21/4/2003
ToString("dd/MM/yy") yields: 21/04/03
ToString("dd/MM/yyyy") yields: 21/04/2003
ToString("dd-MMM-yy") yields: 21-Apr-03
ToString("dd-MMMM-yyyy") yields: 21-April-2003
ToString("yyyy-MM-dd") yields: 2003-04-21
ToString("yy/MM/dd") yields: 03/04/21
ToString("yyyy/MM/dd") yields: 2003/04/21
ToString("dddd, d MMMM yyyy") yields: Monday, 21 April 2003
ToString("d MMMM yyyy") yields: 21 April 2003
ToString("dddd, d MMMM yyyy h:mm tt") yields: Monday, 21 April 2003 10:15 PM
ToString("dddd, d MMMM yyyy H:mm") yields: Monday, 21 April 2003 22:15
ToString("dddd, d MMMM yyyy HH:mm") yields: Monday, 21 April 2003 22:15
ToString("d MMMM yyyy h:mm tt") yields: 21 April 2003 10:15 PM
ToString("d MMMM yyyy H:mm") yields: 21 April 2003 22:15
ToString("d MMMM yyyy HH:mm") yields: 21 April 2003 22:15
ToString("dddd, d MMMM yyyy h:mm:ss tt") yields: Monday, 21 April 2003 10:15:16 PM
ToString("dddd, d MMMM yyyy H:mm:ss") yields: Monday, 21 April 2003 22:15:16
ToString("dddd, d MMMM yyyy HH:mm:ss") yields: Monday, 21 April 2003 22:15:16
ToString("d MMMM yyyy h:mm:ss tt") yields: 21 April 2003 10:15:16 PM
ToString("d MMMM yyyy H:mm:ss") yields: 21 April 2003 22:15:16
ToString("d MMMM yyyy HH:mm:ss") yields: 21 April 2003 22:15:16
ToString("d/MM/yyyy h:mm tt") yields: 21/04/2003 10:15 PM
ToString("d/MM/yyyy H:mm") yields: 21/04/2003 22:15
ToString("d/MM/yyyy HH:mm") yields: 21/04/2003 22:15
ToString("d/MM/yy h:mm tt") yields: 21/04/03 10:15 PM
ToString("d/MM/yy H:mm") yields: 21/04/03 22:15
ToString("d/MM/yy HH:mm") yields: 21/04/03 22:15
ToString("d/M/yy h:mm tt") yields: 21/4/03 10:15 PM
ToString("d/M/yy H:mm") yields: 21/4/03 22:15
ToString("d/M/yy HH:mm") yields: 21/4/03 22:15
ToString("d/M/yyyy h:mm tt") yields: 21/4/2003 10:15 PM
ToString("d/M/yyyy H:mm") yields: 21/4/2003 22:15
ToString("d/M/yyyy HH:mm") yields: 21/4/2003 22:15
ToString("dd/MM/yy h:mm tt") yields: 21/04/03 10:15 PM
ToString("dd/MM/yy H:mm") yields: 21/04/03 22:15
ToString("dd/MM/yy HH:mm") yields: 21/04/03 22:15
ToString("dd/MM/yyyy h:mm tt") yields: 21/04/2003 10:15 PM
ToString("dd/MM/yyyy H:mm") yields: 21/04/2003 22:15
ToString("dd/MM/yyyy HH:mm") yields: 21/04/2003 22:15
ToString("dd-MMM-yy h:mm tt") yields: 21-Apr-03 10:15 PM
ToString("dd-MMM-yy H:mm") yields: 21-Apr-03 22:15
ToString("dd-MMM-yy HH:mm") yields: 21-Apr-03 22:15
ToString("dd-MMMM-yyyy h:mm tt") yields: 21-April-2003 10:15 PM
ToString("dd-MMMM-yyyy H:mm") yields: 21-April-2003 22:15
ToString("dd-MMMM-yyyy HH:mm") yields: 21-April-2003 22:15
ToString("yyyy-MM-dd h:mm tt") yields: 2003-04-21 10:15 PM
ToString("yyyy-MM-dd H:mm") yields: 2003-04-21 22:15
ToString("yyyy-MM-dd HH:mm") yields: 2003-04-21 22:15
ToString("yy/MM/dd h:mm tt") yields: 03/04/21 10:15 PM
ToString("yy/MM/dd H:mm") yields: 03/04/21 22:15
ToString("yy/MM/dd HH:mm") yields: 03/04/21 22:15
ToString("yyyy/MM/dd h:mm tt") yields: 2003/04/21 10:15 PM
ToString("yyyy/MM/dd H:mm") yields: 2003/04/21 22:15
ToString("yyyy/MM/dd HH:mm") yields: 2003/04/21 22:15
ToString("d/MM/yyyy h:mm:ss tt") yields: 21/04/2003 10:15:16 PM
ToString("d/MM/yyyy H:mm:ss") yields: 21/04/2003 22:15:16
ToString("d/MM/yyyy HH:mm:ss") yields: 21/04/2003 22:15:16
ToString("d/MM/yy h:mm:ss tt") yields: 21/04/03 10:15:16 PM
ToString("d/MM/yy H:mm:ss") yields: 21/04/03 22:15:16
ToString("d/MM/yy HH:mm:ss") yields: 21/04/03 22:15:16
ToString("d/M/yy h:mm:ss tt") yields: 21/4/03 10:15:16 PM
ToString("d/M/yy H:mm:ss") yields: 21/4/03 22:15:16
ToString("d/M/yy HH:mm:ss") yields: 21/4/03 22:15:16
ToString("d/M/yyyy h:mm:ss tt") yields: 21/4/2003 10:15:16 PM
ToString("d/M/yyyy H:mm:ss") yields: 21/4/2003 22:15:16
ToString("d/M/yyyy HH:mm:ss") yields: 21/4/2003 22:15:16
ToString("dd/MM/yy h:mm:ss tt") yields: 21/04/03 10:15:16 PM
ToString("dd/MM/yy H:mm:ss") yields: 21/04/03 22:15:16
ToString("dd/MM/yy HH:mm:ss") yields: 21/04/03 22:15:16
ToString("dd/MM/yyyy h:mm:ss tt") yields: 21/04/2003 10:15:16 PM
ToString("dd/MM/yyyy H:mm:ss") yields: 21/04/2003 22:15:16
ToString("dd/MM/yyyy HH:mm:ss") yields: 21/04/2003 22:15:16
ToString("dd-MMM-yy h:mm:ss tt") yields: 21-Apr-03 10:15:16 PM
ToString("dd-MMM-yy H:mm:ss") yields: 21-Apr-03 22:15:16
ToString("dd-MMM-yy HH:mm:ss") yields: 21-Apr-03 22:15:16
ToString("dd-MMMM-yyyy h:mm:ss tt") yields: 21-April-2003 10:15:16 PM
ToString("dd-MMMM-yyyy H:mm:ss") yields: 21-April-2003 22:15:16
ToString("dd-MMMM-yyyy HH:mm:ss") yields: 21-April-2003 22:15:16
ToString("yyyy-MM-dd h:mm:ss tt") yields: 2003-04-21 10:15:16 PM
ToString("yyyy-MM-dd H:mm:ss") yields: 2003-04-21 22:15:16
ToString("yyyy-MM-dd HH:mm:ss") yields: 2003-04-21 22:15:16
ToString("yy/MM/dd h:mm:ss tt") yields: 03/04/21 10:15:16 PM
ToString("yy/MM/dd H:mm:ss") yields: 03/04/21 22:15:16
ToString("yy/MM/dd HH:mm:ss") yields: 03/04/21 22:15:16
ToString("yyyy/MM/dd h:mm:ss tt") yields: 2003/04/21 10:15:16 PM
ToString("yyyy/MM/dd H:mm:ss") yields: 2003/04/21 22:15:16
ToString("yyyy/MM/dd HH:mm:ss") yields: 2003/04/21 22:15:16
ToString("dd MMMM") yields: 21 April
ToString("dd MMMM") yields: 21 April
ToString("ddd, dd MMM yyyy HH’:'mm’:’ss ‘GMT’") yields: Mon, 21 Apr 2003 22:15:16 GMT
ToString("ddd, dd MMM yyyy HH’:'mm’:’ss ‘GMT’") yields: Mon, 21 Apr 2003 22:15:16 GMT
ToString("yyyy’-'MM’-'dd’T'HH’:'mm’:’ss") yields: 2003-04-21T22:15:16
ToString("h:mm tt") yields: 10:15 PM
ToString("H:mm") yields: 22:15
ToString("HH:mm") yields: 22:15
ToString("h:mm:ss tt") yields: 10:15:16 PM
ToString("H:mm:ss") yields: 22:15:16
ToString("HH:mm:ss") yields: 22:15:16
ToString("yyyy’-'MM’-'dd HH’:'mm’:’ss’Z'") yields: 2003-04-21 22:15:16Z
ToString("dddd, d MMMM yyyy h:mm:ss tt") yields: Monday, 21 April 2003 10:15:16PM
ToString("dddd, d MMMM yyyy H:mm:ss") yields: Monday, 21 April 2003 22:15:16
ToString("dddd, d MMMM yyyy HH:mm:ss") yields: Monday, 21 April 2003 22:15:16
ToString("d MMMM yyyy h:mm:ss tt") yields: 21 April 2003 10:15:16 PM
ToString("d MMMM yyyy H:mm:ss") yields: 21 April 2003 22:15:16
ToString("d MMMM yyyy HH:mm:ss") yields: 21 April 2003 22:15:16
ToString("MMMM yyyy") yields: April 2003
ToString("MMMM yyyy") yields: April 2003

Category: ASP.NET, C#  | Leave a Comment
Thursday, September 11th, 2008 | Author: admin

I was getting this error in one of my web apps. I found the solution on a blog by Rana Faisal Munir and credit goes to him for the solution. You can read his entry which explains the reason for the problem in a bit more detail than what I’m mentioning here, but I thought I would post the solution in case it helps someone.

Add the following to your app’s web.config file;

<system.web>
        <hostingEnvironment shadowCopyBinAssemblies="false" />
</system.web>

Category: ASP.NET  | Leave a Comment
Friday, September 05th, 2008 | Author: admin

Over the last few days I have moved over to Windows Vista from the trusty Windows XP OS. One of my main problems when setting up my development environment was when trying to write to the file system through one of my web applications. In XP I set the ASPNET user account to have write permissions on the directory and all was good with the world. So when my web browser showed the access denied error I thought to myself, that’s an easy fix, just give the ASPNET user account write permissions.

When I went looking for the account it was nowhere to be found. It turns out that the ASPNET user account is only installed with the .NET 1.1 framework. with subsequent versions ASP.NET runs under the NETWORK SERVICE account. I simply gave this account write access within my development environment and everything is now good again with the world.

Category: ASP.NET  | Leave a Comment
Friday, September 05th, 2008 | Author: admin

This is actually a very easy thing to do;

using System.IO;

      string filePath = @"c:\temp\MyTest.txt";

      if (File.Exists(filePath))
      {
           Do Something
      } 

Category: ASP.NET, C#  | Leave a Comment