Thursday, December 31, 2015

How to convert string to variable name in Javascript

If you need to create a variable dynamically on the server side and retrieve the value on the client side, use the next example to use the value assigned:

// Declare the variable name
var myStringName = "echoHello";

// Assign a value to the dynamic string variable
window[myStringName] = "Hello from dynamic string variable!";

// Show value
alert(window[myStringName]);

// Or call the variable name
alert(echoHello);


You can even assign a function to the dynamic string variable:

// Declare the variable name
var myStringName = "alertStringVariable";

// Assign a function to the dynamic variable
window[myStringName] = function () {
    alert("Hello from dynamic function variable...");
}

// Execute the function assigned of the variable
alertStringVariable();

Thursday, November 19, 2015

How to stop execution of SSIS Package from SSMS


If your package is running and you want to stop it from the SQL Management Studio follow this steps:

1.- Go to the Integration Services Catalogs and select the SSISDB catalog
2.- Go to the SSISDB catalog Right Click and select the “Active Executions”
3.- You will see the currently running packages, select the desired package then click STOP



How to map network drives programmatically in C#


Follow the next example to map a network drive programmatically:

// Map Network drive
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();

// Notes:
//      Use /C To carry out the command specified by string and then terminates
//      You can omit the passord or username and password
//      Use /PERSISTENT:YES to keep the mapping when the machine is restarted
psi.FileName      = "cmd.exe";
psi.Arguments     = @"/C net use X: \\MyServer\Folder01  /USER:MyDomain\MyUsername MyPassword /PERSISTENT:YES";
psi.WindowStyle   = System.Diagnostics.ProcessWindowStyle.Normal;
process.StartInfo = psi;

process.Start();


Tuesday, November 17, 2015

c# windows application, Error creating window handle (Win32 Exception) or red square with X across


If you are developing a windows application and you get an error like this one:

Application: Tool.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.OutOfMemoryException Stack: at System.Drawing.Graphics.FromHdcInternal(IntPtr) at System.Drawing.Font.GetHeight() at System.Drawing.Font.get_Height() at System.Windows.Forms.Control.get_FontHeight() at System.Windows.Forms.TextBoxBase.get_PreferredHeight() at System.Windows.Forms.TextBoxBase.get_DefaultSize() at System.Windows.Forms.Control..ctor(Boolean) at System.Windows.Forms.TextBoxBase..ctor() at System.Windows.Forms.TextBox..ctor() at System.Windows.Forms.ThreadExceptionDialog..ctor(System.Exception) at System.Windows.Forms.Application+ThreadContext.OnThreadException(System.Exception) at System.Windows.Forms.Control.WndProcException(System.Exception) at System.Windows.Forms.Control+ControlNativeWindow.OnThreadException(System.Exception) at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef) at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32) at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext) at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext) at System.Windows.Forms.Application.Run(System.Windows.Forms.Form) at Tool.Program.Main() 

It is because there is a leak of memory in your program, some objects are not released, usually is because you are building controls dynamically.
This program will generate others like a red square with an X inside:


To identify what is the root of the problem do this:

-Run Process Explorer or the Windows Task Manager to look at the GDI Objects, Handlers, Threads and USERS objects, run your application and see which column is growing really large.
The windows handle limit for your application is 10,000 handles, you can increase this value in the registry but is not the best solution:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\USERProcessHandleQuota

The other limit you have is 66,536 user handles per Windows session and 11,000 for GDI Objects depending on the system.

- Use the application Bear, it shows resource usage of all GDI objects & user objects, and handle count, it is a great tool to identify memory leaks.




- Use the Desktop Heap Monitor to see your limits

-On Visual Studio break all the exceptions (Control/Alt + E) then check the Common Language Run-time Exceptions, in order to get all the exceptions


Solutions:
-Dispose you objects manually, here you have a good article about it.
-Reuse components specially  the GDI Objects; for example if you are creating dynamically objects like ToolTip, they stay in memory, they don't get disposed.


Wednesday, November 11, 2015

How to scrip all the indexes on a SQL database?

The script below creates a store procedure that generates the indexes from a database, it was taken from SqlServerCentral.com.



IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'utils'
AND SCHEMA_OWNER = 'dbo')
BEGIN
    EXEC('CREATE SCHEMA utils AUTHORIZATION dbo')
END
GO

IF NOT EXISTS(SELECT 1
                FROM INFORMATION_SCHEMA.ROUTINES
               WHERE ROUTINE_NAME = 'GenerateIndexesScript'
                 AND ROUTINE_TYPE = N'PROCEDURE')
BEGIN
    EXEC ('CREATE PROCEDURE [utils].[GenerateIndexesScript] AS BEGIN SELECT 1 END')
END
GO

/*
Adapted from http://www.sqlservercentral.com/Forums/Topic401784-562-2.aspx (by Jonathan AC Roberts. )

Modifications 10/06/2010 By R.Doering - http://sqlsolace.blogspot.com

       1) Changed Schema of routine to Utils
       2) Changed Name from INFGenerateIndexesScript to GenerateIndexesScript
       3) Added Schemas to script
       4) Reformatted for clarity

--  Usage: EXEC utils.GenerateIndexesScript 1, 0, 0
*/
ALTER PROCEDURE utils.GenerateIndexesScript
(
    @IncludeFileGroup  bit = 1,
    @IncludeDrop       bit = 1,
    @IncludeFillFactor bit = 1
)
AS

BEGIN
    -- Get all existing indexes, but NOT the primary keys
    DECLARE Indexes_cursor CURSOR
        FOR SELECT
                                  SC.Name                    AS     SchemaName
                                  , SO.Name                  AS     TableName
                    , SI.Object_Id     AS       TableId
                                  , SI.[Name]         AS     IndexName
                                  , SI.Index_ID       AS     IndexId
                                  , FG.[Name]       AS FileGroupName
                                  , CASE WHEN SI.Fill_Factor = 0 THEN 100 ELSE SI.Fill_Factor END  Fill_Factor
              FROM sys.indexes SI
              LEFT JOIN sys.filegroups FG
                     ON SI.data_space_id = FG.data_space_id
              INNER JOIN sys.objects SO
                                  ON SI.object_id = SO.object_id
                       INNER JOIN sys.schemas SC
                                  ON SC.schema_id = SO.schema_id
             WHERE ObjectProperty(SI.Object_Id, 'IsUserTable') = 1
               AND SI.[Name] IS NOT NULL
               AND SI.is_primary_key = 0
               AND SI.is_unique_constraint = 0
               AND IndexProperty(SI.Object_Id, SI.[Name], 'IsStatistics') = 0
             ORDER BY Object_name(SI.Object_Id), SI.Index_ID

    DECLARE @SchemaName           sysname
    DECLARE @TableName                   sysname
    DECLARE @TableId                            int
    DECLARE @IndexName                          sysname
    DECLARE @FileGroupName sysname
    DECLARE @IndexId                            int
    DECLARE @FillFactor                         int

    DECLARE @NewLine nvarchar(4000)     SET @NewLine = CHAR(13) + CHAR(10)
    DECLARE @Tab                  nvarchar(4000)     SET @Tab = Space(4)

    -- Loop through all indexes
    OPEN Indexes_cursor

    FETCH NEXT
     FROM Indexes_cursor
     INTO @SchemaName, @TableName, @TableId, @IndexName, @IndexId, @FileGroupName, @FillFactor

    WHILE (@@Fetch_Status = 0)
        BEGIN

            DECLARE @sIndexDesc   nvarchar(4000)
            DECLARE @sCreateSql   nvarchar(4000)
            DECLARE @sDropSql            nvarchar(4000)

            SET @sIndexDesc = '-- Index ' + @IndexName + ' on table ' + @TableName
            SET @sDropSql = 'IF EXISTS(SELECT 1' + @NewLine
                          + '            FROM sysindexes si' + @NewLine
                          + '            INNER JOIN sysobjects so' + @NewLine
                          + '                   ON so.id = si.id' + @NewLine
                          + '           WHERE si.[Name] = N''' + @IndexName + ''' -- Index Name' + @NewLine
                          + '             AND so.[Name] = N''' + @TableName + ''')  -- Table Name' + @NewLine
                          + 'BEGIN' + @NewLine
                          + '    DROP INDEX [' + @IndexName + '] ON [' + @SchemaName + '].[' + @TableName + ']' + @NewLine
                          + 'END' + @NewLine

            SET @sCreateSql = 'CREATE '

            -- Check if the index is unique
            IF (IndexProperty(@TableId, @IndexName, 'IsUnique') = 1)
                BEGIN
                    SET @sCreateSql = @sCreateSql + 'UNIQUE '
                END
            --END IF
            -- Check if the index is clustered
            IF (IndexProperty(@TableId, @IndexName, 'IsClustered') = 1)
                BEGIN
                    SET @sCreateSql = @sCreateSql + 'CLUSTERED '
                END
            --END IF

            SET @sCreateSql = @sCreateSql + 'INDEX [' + @IndexName + '] ON [' + @SchemaName + '].[' + @TableName + ']' + @NewLine + '(' + @NewLine

            -- Get all columns of the index
            DECLARE IndexColumns_cursor CURSOR
                FOR SELECT SC.[Name],
                           IC.[is_included_column],
                           IC.is_descending_key
                      FROM sys.index_columns IC
                     INNER JOIN sys.columns SC
                             ON IC.Object_Id = SC.Object_Id
                            AND IC.Column_ID = SC.Column_ID
                     WHERE IC.Object_Id = @TableId
                       AND Index_ID = @IndexId
                     ORDER BY IC.key_ordinal

            DECLARE @IxColumn                   sysname
            DECLARE @IxIncl                            bit
            DECLARE @Desc                              bit
            DECLARE @IxIsIncl                                 bit     SET @IxIsIncl = 0
            DECLARE @IxFirstColumn       bit     SET @IxFirstColumn = 1

            -- Loop through all columns of the index and append them to the CREATE statement
            OPEN IndexColumns_cursor
            FETCH NEXT
             FROM IndexColumns_cursor
             INTO @IxColumn, @IxIncl, @Desc

            WHILE (@@Fetch_Status = 0)
                BEGIN
                    IF (@IxFirstColumn = 1)
                        BEGIN
                            SET @IxFirstColumn = 0
                        END
                    ELSE
                        BEGIN
                            --check to see if it's an included column
                            IF (@IxIsIncl = 0) AND (@IxIncl = 1)
                                BEGIN
                                    SET @IxIsIncl = 1
                                    SET @sCreateSql = @sCreateSql + @NewLine + ')' + @NewLine + 'INCLUDE' + @NewLine + '(' + @NewLine
                                END
                            ELSE
                                BEGIN
                                    SET @sCreateSql = @sCreateSql + ',' + @NewLine
                                END
                            --END IF
                        END
                    --END IF

                    SET @sCreateSql = @sCreateSql + @Tab + '[' + @IxColumn + ']'
                    -- check if ASC or DESC
                    IF @IxIsIncl = 0
                        BEGIN
                            IF @Desc = 1
                                BEGIN
                                    SET @sCreateSql = @sCreateSql + ' DESC'
                                END
                            ELSE
                                BEGIN
                                    SET @sCreateSql = @sCreateSql + ' ASC'
                                END
                            --END IF
                        END
                    --END IF
                    FETCH NEXT
                     FROM IndexColumns_cursor
                     INTO @IxColumn, @IxIncl, @Desc
                END
            --END WHILE
            CLOSE IndexColumns_cursor
            DEALLOCATE IndexColumns_cursor

            SET @sCreateSql = @sCreateSql + @NewLine + ') '

            IF @IncludeFillFactor = 1
                BEGIN
                    SET @sCreateSql = @sCreateSql + @NewLine + 'WITH (FillFactor = ' + Cast(@FillFactor as varchar(13)) + ')' + @NewLine
                END
            --END IF

            IF @IncludeFileGroup = 1
                BEGIN
                    SET @sCreateSql = @sCreateSql + 'ON ['+ @FileGroupName + ']' + @NewLine
                END
            ELSE
                BEGIN
                    SET @sCreateSql = @sCreateSql + @NewLine
                END
            --END IF

            PRINT '-- **********************************************************************'
            PRINT @sIndexDesc
            PRINT '-- **********************************************************************'

            IF @IncludeDrop = 1
                BEGIN
                    PRINT @sDropSql
                    PRINT 'GO'
                END
            --END IF

            PRINT @sCreateSql
            PRINT 'GO' + @NewLine  + @NewLine

            FETCH NEXT
             FROM Indexes_cursor
             INTO @SchemaName, @TableName, @TableId, @IndexName, @IndexId, @FileGroupName, @FillFactor
        END
    --END WHILE
    CLOSE Indexes_cursor
    DEALLOCATE Indexes_cursor
END

GO