Sunday, December 23, 2012

Password in SQL


DECLARE @PWD varbinary(128)
SELECT @PWD = PWDENCRYPT ( 'password' )
SELECT @PWD
-- Correct
SELECT PWDCOMPARE('password', @Pwd )  -- 1
-- Failed
SELECT PWDCOMPARE('bla-bla-bla', @Pwd )  -- 0

Tuesday, December 18, 2012

Shrink DB log file

USE dbname;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE dbname
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (2, 1);  -- here 2 is the file ID for trasaction log file,you can also mention the log file name (dbname_log)
GO
-- Reset the database recovery model.
ALTER DATABASE dbname
SET RECOVERY FULL;
GO

Sunday, October 28, 2012

Validation is a boxing value is default

I very like my lovely extension IsDefault but unpossible to use it where a value is boxing. A cool feature of C#  default(...)  cannot help because a argument required Type. After long research was created small solution:

static bool IsDefault(object o)
{
    if (o == null)
    {
        return true;
    }
    //Check is type os object is ValueType
    if (o.GetType().IsValueType)
    {
        return Activator.CreateInstance(o.GetType()).Equals(o);
    }
    //ReferenceType
    return false;
}


Also can be implemented as extension.

Thursday, October 18, 2012

An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'XXX' cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.

A web.config of project include next rows:
...
<extensions>
    <behaviorExtensions>
        <add name="ELEMENT" type="YYY.ZZZ,YYY.XXX, Version=1.0.0.0,Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
</extensions>
...
<behavior name="BehaviorName">
          ELEMENT />
</behavior>

In the start project WCF is throwing exception and message:
"An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'ELEMENT' cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions."

A problem exists in file AssemblyInfo of YYY.XXX and not in config.
YYY.XXX change build number in every compilation but WCF is require a full name of assembly (AssemblyQualifiedName) and exact assembly file version number.

Open AssemblyInfo and see last 2 rows:
[assembly: AssemblyVersion(...)]
[assembly: AssemblyFileVersion(...)]

If a code looks like this:
[assembly: AssemblyVersion(5.0.1.0)]
[assembly: AssemblyFileVersion(5.0.1.0)]

You not reading current article but if acode looks like this:

[assembly: AssemblyVersion(5.0.1.0)]
[assembly: AssemblyFileVersion(5.0.1.*)]

This is a time to change it and resolve a problem. Enjoy.



Sunday, August 12, 2012

Disable the Browser Back Button

 if (window.history) {  
 window.history.forward(1);  
 }  
A script tested in IE 9, Chrome and FireFox.


From here

Sunday, August 05, 2012

Culture in .NET. All in one

By inspiration from article Hidden Gems inside .Net Classes I created static class that include short methods for quick retrieval object or properties of Cultures and Time Zones. All data based on build-in functionality of Framework 4.0


 public static class Culture
    {
        #region Consts
        private static readonly StringDictionary cultureDetails;
        private static readonly ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones(); 
        #endregion
        
        /// <summary>
        /// Ctor
        /// </summary>
        static  Culture()
        {
            #region Init culture datails
            cultureDetails = new StringDictionary();
            foreach (CultureInfo cultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            {
                RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);

                if (!cultureDetails.ContainsKey(regionInfo.EnglishName))
                {
                    cultureDetails.Add(regionInfo.EnglishName, regionInfo.Name);
                }
            } 
            #endregion
        }
        /// <summary>
        /// Get culture name by Country
        /// </summary>
        /// <param name="countryName"></param>
        /// <returns></returns>
        public static string GetCulture(string countryName)
        {
            return cultureDetails.ContainsKey(countryName) ? cultureDetails[countryName] : string.Empty;
        }
        /// <summary>
        /// Get Culture info by culture name
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static CultureInfo GetCultureInfo(string cultureName)
        {
            return CultureInfo.GetCultures(CultureTypes.SpecificCultures).FirstOrDefault(item => item.Name == cultureName);
        }
        /// <summary>
        /// Get month names by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static string[] GetMonths(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? null : region.DateTimeFormat.MonthNames;
        }
        /// <summary>
        /// Get day names by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static string[] GetDays(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? null : region.DateTimeFormat.DayNames;
        }
        /// <summary>
        /// Get first day of week by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static DayOfWeek GetFirstDayOfWeek(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? default(DayOfWeek) : region.DateTimeFormat.FirstDayOfWeek;
        }
        /// <summary>
        /// Get datetime format by culture
        /// </summary>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        public static string GetDateTimeFormat(string cultureName)
        {
            var region = GetCultureInfo(cultureName);
            return region == null ? string.Empty : region.DateTimeFormat.FullDateTimePattern;
        }
        /// <summary>
        /// Get TimeZone info by DisplayName
        /// </summary>
        /// <param name="displayName"></param>
        /// <returns></returns>
        public static TimeZoneInfo GetTimeZoneByDisplayName(string name)
        {
           return timeZones.FirstOrDefault(item => item.DisplayName == name);
        }
        /// <summary>
        /// Get TimeZone info by Standart Name
        /// </summary>
        /// <param name="displayName"></param>
        /// <returns></returns>
        public static TimeZoneInfo GetTimeZoneByStandartName(string name)
        {
            return timeZones.FirstOrDefault(item => item.StandardName == name);
        }
    }

Sunday, January 22, 2012

Schedule Daily backup of database in MS-SQL Server 2008

1. Open Management Studio.
2. Under "Mangement"  select Maintenance Plans.
3. Right click  and choose New Plan (it will ask for name).
4. Double click on Subplan_1 and change it to your name.
5. Drag from toolbox "Back Up Database Task" control.
6. Right click on it and choose Edit.
7. Choose Backup Type, Databases  and change more fields.
8.OK
9. Click on Job Schedule (in end of row) and set backup date/time.
10. Save plan.
11. Under "Mangement" right click on your job and choose "Execute" for validation.


Enjoy!