Friday, September 30, 2011

Converting and Formatting DataSet Strings

Consider the following. You have a DataSet and you need to assign the contest of this dataset to another string. In the example below, several TextBoxes.

You could do the below to convert the non-string data in the DataSet row to a String and then apply a String Format.

TextBoxDecimal.Text = ((decimal)lProjectsDS.Tables[0].Rows[0]["Budget"]).ToString("c");
TextBoxDouble.Text = ((double)lProjectsDS.Tables[0].Rows[0]["MaxHours"]).ToString("0.00");
TextBoxDate.Text = ((DateTime)lProjectsDS.Tables[0].Rows[0]["ExpDate"]).ToString("d");

The ToString format work as follows:
- Format: "c" converts to a currency using the default locate.
- Format: "0.00" converts the number so it has two decimals, if no decimals found then displays 00
- Format: "d" shortdate using the default locate.

You could also apply formatting to the TextBoxes or the target string, however in my case the need was to format the data you receive right out of the Dataset.

Happy Coding,
Will




Thursday, September 29, 2011

How to Find the UserID using Membership

I have a table which is driven by the UserID which is assigned when a new user is added to the database using Membership.

My application needs to retrieve the UserID on the fly in order to be able to find the correct record.
One way to do this is the following:

Membership.GetUser().ProviderUserKey.ToString();

I hope this helps,
Will




Saturday, September 24, 2011

Where is the iPhone, iPad, MAC Address?

Wondering where is the MAC address of your Apple mobile device?
Go to: Settings -> General -> About

The Wi-Fi Address is the device's MAC Address

Will

How to Format an Eval Statement to Display a Date using Date Locale

I need to format a DateTime value that I'm using inside an ItemTemplate via an Eval expression, so it displays a short date using the locale. I tried working with the format syntax on the Eval statement without any luck.
I came across the following syntax to format an Eval expression so it displays a short date formatted according to the user's locale:

<%#((DateTime)Eval("ExpDate")).ToString("d")%>

The key is casting Eval as a DateTime which you can then convert to String. At this point you can take advantage of the ToString format "d" which uses a locale short date format.
This works for ASP.Net 2.0 and later, it will save you having to create a function to determine the users locale.
Will





Wednesday, September 21, 2011

Using ASP.Net Profile Feature in a Web Application Project

So, I decided to take advantage of the Profile feature of .NET to store some personal data per user. Shouldn't be a big deal right? After all, you just call the appropriate assembly and then, you should be able to invoke Profile.Save() and like magic the data is now in your ASP database on the Profiles table.

This is when I came across a huge set of posts and threads about how complex and confusing it can be to implement the Profile feature if you have a Web Application Project.

Among the confusing, credit should be due to Lee Dumond and Scott Gu as usual for posting some awesome examples in their blogs.

By following Lee's blog, below is my implementation of the same technique to be able to use Profiles in my Web Application Project. I thought it would be a good idea to post it, in order to give you a second example.

Step 1. Created a Class named ProfileInfo which exposes the properties that I want to save in my profile, in this case, let's work with two FirstName and Last Name

namespace Project1.Account
{
    [Serializable]
    public class ProfileInfo
    {
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    }
}

Step 2. Created a second Class named wProfile, this class is inherited from the ProfileBase class and will be used to expose the profile functionality on other pages in the site.

using System.Web;
using System.Web.Profile;
namespace Project1.Account
{
    public class wProfile : ProfileBase
    {
 
        public ProfileInfo ProfileInfo
        { 
            get { return (ProfileInfo) GetPropertyValue("ProfileInfo"); }       
        } 
 
        public static wProfile GetProfile() 
        { 
            return (wProfile) HttpContext.Current.Profile; 
        } 
 
        public static wProfile GetProfile(string userName) 
        { 
            return (wProfile) Create(userName); 
        }  
    }
}

Step 3. Modify your Web.config file to implement a SQLProfileProvider. Notice the "inherits" on the defaultProvider definition. Also noticed that the connection ApplicationServices was previously defined.

    <profile defaultProvider="AspNetSqlProfileProvider" inherits="Project1.Account.wProfile"> 
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/wMyApp"/>
      </providers>
    </profile>

Step 4. Implementing the logic in my ASPX pages

        protected void Page_Load(object sender, EventArgs e)
        {
            wProfile lProfile = wProfile.GetProfile(Membership.GetUser().UserName);
            Label1.Text = "Welcome, " + lProfile.ProfileInfo.FirstName + " " + lProfile.ProfileInfo.LastName;
        }

Happy Coding, Hope this helps,
Will