Monday, June 23, 2014

Autoclose Message box in Window Forms C#


Helper Class:

public class AutoClosingMessageBox
    {
        System.Threading.Timer _timeoutTimer;
        string _caption;
        AutoClosingMessageBox(string text, string caption, int timeout)
        {
            _caption = caption;
            _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
                null, timeout, System.Threading.Timeout.Infinite);
            MessageBox.Show(text, caption);
        }
        public static void Show(string text, string caption, int timeout)
        {
            new AutoClosingMessageBox(text, caption, timeout);
        }
        void OnTimerElapsed(object state)
        {
            IntPtr mbWnd = FindWindow(null, _caption);
            if (mbWnd != IntPtr.Zero)
                SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            _timeoutTimer.Dispose();
        }
        const int WM_CLOSE = 0x0010;
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
    }

Calling:

//Three parameters: Text to be shown, Caption and message timeout in milliseconds
AutoClosingMessageBox.Show("Message to be shown", "Caption", 5000);

Monday, June 2, 2014

Change Language in SQL

SQL Query


Set LANGUAGE Norwegian

Select Datename(month, getdate())


Output:


juni

Wednesday, May 21, 2014

Another way to remove the specific item From n-dimensional array using Jquery


//Array 
var elementsArray = [
        { name: 'First Element', id: 1},
        { name: 'Second Element', id: 2 },
        { name: 'Third Element', id: 3 },
       ];


//Jquery Function to remove the current object from the array
    function RemoveElementFromArray(array, id) {
        if (array != null && array.length > 0) {
            array = $.grep(array, function (value) {
                return value.id != id;
            });
            return array;
        }
        else {
            return null;
        }
    }


//Calling
function remove()
{
//For instance, we want to remove the 2nd element having ID 2

elementsArray  =  RemoveElementFromArray(elementsArray, 2);
for(int i=0; i<elementsArray.length; i++)
{
alert(elementsArray[i].name);
}
}

Tuesday, May 6, 2014

Pass DataTable to SQL Server Stored Procedure using C# ADO.Net

Database:

//Create new type (i.e. Table data type) as input parameter to be used in ADO.Net SQL Command

CREATE TYPE DtFile as Table
(
    Id int
)

//Create Procedure and use the above created type in it.

Create Proc dbo.UspInsertFileData
(
@FileId dbo.DtFile READONLY
)
AS
Begin
        INSERT INTO MainFileData (FileId, AddedOn)
        SELECT Id, GETDATE() From dbo.DtFile
End

C# Asp.Net:

Data Access Layer:


public static int InsertFileData(DataTable dtFileIds)
{
         var sqlConnectionString = ConfigurationManager.AppSettings["fileConnection"];
         var sqlParameters = new SqlParameter[1];
         sqlParameters[0] = new SqlParameter
                                   {
                                       ParameterName = @"FileId",
                                       SqlDbType = SqlDbType.Structured,
                                       Value = fileTreeIds,
                                       TypeName = @"DtFile"
                                   };
          var result = SqlHelper.ExecuteScalar(sqlConnectionString,  CommandType.StoredProcedure,  @"UspInsertFileData" , sqlParameters);          return Convert.ToInt32(result);
}

Presentation Layer:

protected void BtnClickAddFileData(object sender, EventArgs e)
{
      var fileIds = new[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
      var dtFileData = new DataTable();
      if (fileIds.Length > 0)
      {
                /* Make Sure to keep the name of the data column same as given in the SQL User-Defined  type */
                dtFileData.Columns.Add(new DataColumn(@"Id", typeof(int)));
                foreach (var id in fileIds)
                {
                           var dr = dtFileData.NewRow();
                          dr["FileTreeId"] = item;
                          dtFileData.Rows.Add(dr);
                          dtFileData.AcceptChanges();
                }
       }
       var result = Dal.InsertFileData(dtFileData);
}



Tuesday, April 29, 2014

Regular Expression for Password

var Password_REGEX = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_*#&%@]).{8,}$/;

It meets the following criteria:
- Minimum 8 charaters
- At least one number, one alphabet and one special charater.

Thursday, April 17, 2014

Explicitly Set the MAX Size to SQLParameter Object C#

ByDefault, it will take max 400 characters. E.g.
sqlParameters[1] = new SqlParameter("@usernames", usernames);

We can explicitly Set the MAX Size to SQLParameter Object by the following way:
sqlParameters[1] = new SqlParameter("@usernames", SqlDbType.NVarChar, -1,
                                                usernames);


Wednesday, April 16, 2014

Use Multiple With Clause in one SQL Query

;With T (Name)
AS
(Select Top 1 Test1Name From Test), 
T2 (Name2) as
(
Select Top 1 Test2name From Test2
),
T3 (Name3) as
(
Select TOP 1 Test3Name From [Test3]
)
Select T.Name, T2.Name2, T3.Name3 From T, T2, T3