Thursday, February 7, 2013

Helper Functions and ASPX

For several years I have compiled a library of helper functions to assist with recurring coding problems and repetitive tasks in my daily work.  Depending on the project, this helper class will grow or shrink but it always seems to find a home in my work.

So I thought I would share the core of my helper class and talk about some of the ideas behind it.

Lets start with errors.  The following function works OK.  It gets me the gyst of the error most of the time, but has the fatal flaw of not doing anything in the case it fails.  Luckily this is used in conjunction with logging so it has never really been a problem.

        public static void SendError(Exception ex, string sIPAddress, string strRequest = "")
        {
            try
            {
                MembershipUser objUser = Membership.GetUser();
                MailMessage message = new MailMessage();                message.From = new MailAddress(admin@homeschooloffice.com);
                message.To.Add(new MailAddress(support@homeschooloffice.com));
                message.Subject = string.Format("{0}-{2}:{1}", objUser.UserName, ex.Message, sIPAddress);
                message.Body = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\r\n{6}", ex.Message,
                                                                ex.Source,
                                                                ex.TargetSite.ToString(),
                                                                objUser.UserName,
                                                                strRequest,
                                                                sIPAddress,
                                                                ex.StackTrace);                SmtpClient client = new SmtpClient();
                client.Send(message);            }
            catch (Exception)
            {            }
        } If you use aspnet membership controls this next function is nice:          public static Guid UserID
        {
            get
            {
                MembershipUser objUser = Membership.GetUser();
                return new Guid(objUser.ProviderUserKey.ToString());
            }
        } Need to encrypt pesky ids or other information?  http://msdn.microsoft.com/en-us/library/ms972969.aspx provides some other guidelines that should be followed when working in web pages, but this was one take away:         public static string EncodeText(int text)
        {
            return EncodeText(text.ToString());
        }        public static string EncodeText(string text)
        {
            StringWriter writer = new StringWriter();
            LosFormatter formatter = new LosFormatter();
            formatter.Serialize(writer, text);
            return writer.ToString();
        }        public static string DecodeText(string text)
        {
            if (string.IsNullOrEmpty(text)) return "";            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(text).ToString();
        } Lastly,  I have a few functions that follow this general idea.  If you want to bind common controls with default values or just make them one liners I suggest using functions like this for repeaters and similar variants, listboxes, dropdowns, etc.         public static void SetDropDownList(ref DropDownList ddlList, object DataSource, string TextField, string ValueField)
        {
            SetDropDownList(ref ddlList, DataSource, TextField, ValueField, "____________________", "");
        }         public static void SetDropDownList(ref DropDownList ddlList, object DataSource, string TextField, string ValueField, string DefaultText, string DefaultValue)
        {
            if (ddlList == null) return;
            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem();
            li.Value = DefaultValue;
            li.Text = DefaultText;            ddlList.DataSource = DataSource;
            ddlList.DataTextField = TextField;
            ddlList.DataValueField = ValueField;
            ddlList.DataBind();
            ddlList.Items.Insert(0, li);
        }
 There are a few other items that I typically keep in my helper class (like functions to clear forms) but this covers most of the core.  I hope this helps.

No comments:

Post a Comment