Friday, July 15, 2011

Using Enums

References:

1 . EnumHelper - Getting a Friendly Description from an Enum

2. Enum: Binding to the Description Attribute

3)Enum Helper Get enum values to bind to a listcontrol
///
/// Get all the values of an enumeration of Type enumType and bind to a listcontrol
///

/// Enumeration type/// List control /// Array of objects
public static void BindEnumToListControl(Type enumType, ListControl lstControl)
{
Dictionary enumslist = new Dictionary();
foreach (int enumValue in Enum.GetValues(enumType))
{
enumslist.Add(enumValue,Enum.GetName(enumType, enumValue));
}
lstControl.DataTextField = "Value";
lstControl.DataValueField = "Key";
lstControl.DataSource = enumslist;
lstControl.DataBind();
}

4)To convert integer to enum
EnumType var = (EnumType)(Convert.ToInt16(Enum_value);

5)Get enum integer value -- check
int i = (byte)Enum.Parse(typeof(EnumTypestring), volume_value));
or
int i = (byte)EnumType.Item;

Feel free to leave your comments!!

Thursday, July 14, 2011

Learn asp.net stepbystep tutorials

Reference:
http://www.learn-asp.net/asptutorials/DetailsView.aspx

Logging in .net

1)Good logging module - like log4net
2)Good logging format code so that reading the log to find out
error is easier-- find out!!
eg: datetime :classname :method Name :message

3) Simple logging code is shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace Common
{
    public class Logger    {
        private static string _logFileName = string.Empty;
 
        /// <summary>        /// Create a statis logger instance for your project        /// </summary>        /// <param name="filename">Log file name</param>        public Logger(string filename = "log.txt")
        {
            _logFileName = filename;
        }
 
        /// <summary>        /// Used to log an exception information        /// </summary>        /// <param name="ex"></param>        /// <param name="classname"></param>        /// <param name="methodName"></param>        /// <param name="msg"></param>        public void WriteEx(Exception ex, string classname = ""string methodName = ""string msg = "")
        {
            try            {
                StreamWriter sw = new StreamWriter(_logFileName, true);
                sw.WriteLine(string.Format("{0} : {1} : {2} : {3} : {4} ", 
                              DateTime.UtcNow.ToShortDateString(), classname, methodName, ex.Message, msg));
            }
            catch            {
                //Do nothing            }
        }
 
        /// <summary>        /// Used to log any information        /// </summary>        /// <param name="ex"></param>        /// <param name="classname"></param>        /// <param name="methodName"></param>        /// <param name="msg"></param>        public void WriteMsg(string msg, string classname = ""string methodName = "")
        {
            try            {
                StreamWriter sw = new StreamWriter(_logFileName, true);
                sw.WriteLine(string.Format("{0} : {1} : {2} : {3} ",
                              DateTime.UtcNow.ToShortDateString(), classname, methodName, msg));
 
            }
            catch            {
                //Do nothing            }
        }
    }
}



to be continued...

Friday, July 1, 2011

What's the best practice for primary keys in tables?

1. Primary keys should be as small as necessary. Prefer a numeric type because numeric types are stored in a much more compact format than character formats. This is because most primary keys will be foreign keys in another table as well as used in multiple indexes. The smaller your key, the smaller the index, the less pages in the cache you will use.
When dealing with "small" databases this stuff doesn't matter so much. But when you deal with large db's all of the little things matter. Just imagine if you have 1 billion rows with int or long pk's compared to using text or guid's. There's a huge difference!

Reference:
1 .http://stackoverflow.com/questions/337503/whats-the-best-practice-for-primary-keys-in-tables

Are You A Good Developer?

A must read aticle at --> http://www.dotnetcurry.com/ShowArticle.aspx?ID=182

Devops links

  Build Versioning in Azure DevOps Pipelines