Tuesday, February 15, 2011

Code to do a simple regex extraction

image

I like about the above:

  • one line does the match and one line does the extraction of the match and the conversion to the target type
  • to me, it reads clearly
  • it seems like a good use of “var” because I don’t care much that “reg” is a GroupCollection

Used LINQ to Coalesce Form Keys

image

Display Multiple Currency Symbols in C#

If you have a money amount:

decimal money = 107.04;

And know the code for a culture:

string cultureName = “en-gb”; // display currency in pounds

Create string containing a culture specific currency:

string formattedCurrency = String.Format(CultureInfo.CreateSpecificCulture(map[cur]), "{0:C}", decimal);

The table is on msdn: http://msdn.microsoft.com/en-us/library/ks7d2abt.aspx

Then you can insert that string into HTML (tested on IE 8):

image

Monday, February 14, 2011

Sql Server 2008 Security/Logins

I’m using windows authentication in an intranet.

The way I am configuring permissions is:

1. Add a login for an individual or group to the instance level security.

2. Set the user mappings for databases I wish to give access, assigning roles.

From: http://www.techrepublic.com/article/understanding-roles-in-sql-server-security/1061781

Predefined database roles:

  • db_owner: Members have full access.
  • db_accessadmin: Members can manage Windows groups and SQL Server logins.
  • db_datareader: Members can read all data.
  • db_datawriter: Members can add, delete, or modify data in the tables.
  • db_ddladmin: Members can run dynamic-link library (DLL) statements.
  • db_securityadmin: Members can modify role membership and manage permissions.
  • db_bckupoperator: Members can back up the database.
  • db_denydatareader: Members can’t view data within the database.
  • db_denydatawriter: Members can’t change or delete data in tables or views.

image

Captain’s Log

This morning I added a feature to the campaign page to copy info to clipboard.

I needed to review the details surrounding how to wire a client side click to individually rendered elements of the template.

Found How to dynamically set control IDs inside a repeater template?

How does one copy to the clipboard via JavaScript?

Found (IE only) http://www.htmlgoodies.com/beyond/javascript/article.php/3458851/Click-Its-Copied.htm

My Code: Copy-To-Clipboard client side click handler.

image 

What can I use for a copy-to-clipboard icon?

A: http://www.iconarchive.com/show/must-have-icons-by-visualpharm/Copy-icon.html
Now the page has the functional copy icon in it:
image 
My Code: ASP.NET logic for putting a copy-to-clipboard icon after the short description of the campaign.
image 
Issue: The feature is not fully useful until I change the ETL to bring in the entire description as opposed to the first 255 characters only. Pros and Cons?
 

Monday, January 24, 2011

Code and Example of FormSerializer utility class.

Scenario

You are writing a windows forms application and you want a simple way to save the location (and possibly other properties) of your WinForms.

Code for FormSerializer

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace EomApp1.UI
{
    public class FormSerializer
    {
        public static void Serialize(Form f, string path)
        {
            XmlSerializer xs = new XmlSerializer(typeof(FormProps));
            using (Stream s = File.Create(path))
            {
                try
                {
                    FormProps props = new FormProps();
                    props.x = f.Location.X;
                    props.y = f.Location.Y;
                    xs.Serialize(s, props);
                }
                catch
                {
                }
            }
        }
        public static void Deserialize(Form f, string path)
        {
            XmlSerializer xs = new XmlSerializer(typeof(FormProps));
            if (File.Exists(path))
            {
                using (Stream s = File.OpenRead(path))
                {
                    try
                    {
                        FormProps props = (FormProps)xs.Deserialize(s);
                        f.Location = new Point(props.x, props.y);
                    }
                    catch
                    {
                    }
                }
            }
        }
        public class FormProps
        {
            public int x;
            public int y;
        }
    }
}