Tuesday, July 2, 2013

Extracting XPAH from xmlNode instace

Here is a very simple and elegant function to extract the xpath string of a xml node instance:

static string GetXpath(XmlNode node)
    {
        if (node.Name == "#document")
            return String.Empty;
        return GetXpath(node.SelectSingleNode("..")) + "/" +  (node.NodeType == XmlNodeType.Attribute ? "@":String.Empty) + node.Name;
    }

credit: http://stackoverflow.com/questions/241238/how-to-get-xpath-from-an-xmlnode-instance-c-sharp

Tuesday, February 5, 2013

c# bring another application to foreground

If you ever need to bring another windows process to the foreground or in other words from your code, set focus on another application (not your current running app) and bring it to the front of the screen here is what you need to do:

1) Import the following DLLS:


        [DllImport("user32.dll")]
        public static extern bool ShowWindowAsync(HandleRef hWnd, int nCmdShow);
        [DllImport("user32.dll")]
         public static extern bool SetForegroundWindow(IntPtr WindowHandle);
        public const int SW_RESTORE = 9;


* Don't forget to add the using statement:
       using System.Runtime.InteropServices;

2) Identify the process you want to bring to the front and set the focus on it's window handle:

private void FocusProcess(string procName)
    {
        Process[] objProcesses = System.Diagnostics.Process.GetProcessesByName(procName);        if (objProcesses.Length > 0)
        {
            IntPtr hWnd = IntPtr.Zero;
            hWnd = objProcesses[0].MainWindowHandle;
            ShowWindowAsync(new HandleRef(null,hWnd), SW_RESTORE);
             SetForegroundWindow(objProcesses[0].MainWindowHandle);
        }
    }







Thursday, December 13, 2012

Setting up GoDaddy's account on iPhone, iPad and other Mobile Devices

I know this is not that "technical" post but i've had to battle with this for too long so i thought i could save some people some time. 

If the GoDaddy instructions for the iPad email client do not work for any reason please review your configuration and port settings as follows:

Incoming IMAP without SSL should use port 143
Incoming IMAP with SSL should use port 993
Incoming POP without SSL should use port 110
Incoming POP with SSL should use port 995
Outgoing settings without SSL should use ports 25, 80, 3535
Outgoing settings With SSL should use port 465


GoDaddy's web based email interface can be accessed at http://email.godaddy.com


Tuesday, August 28, 2012

VS 2010 Cool Debugger Tips & Tricks

Checkout this cool post about some really helpful tips & tricks for debugging in VS2010
VS 2010 Debugger Improvements (BreakPoints, DataTips, Import/Export)

Hope it helps you as much as it did me.

Wednesday, July 18, 2012

Server 2008 scheduled task not working - action "C:\Windows\SYSTEM32\cmd.exe" with return code 1

MS server 2008 / R2 scheduled task the older server versions. Especially if you are trying to create the tasks using a command line batch script.

In server 2000 we used to run the following command and it would be enough:
schtasks /Create /S [machine name] /RU [domain user] /RP [password] /SC ONCE /TN [task name] /TR [batch name i.e. DoSomething.bat] /ST [StartTime] /SD [RunDate]

In the server 2008 world the above command will get scheduled and executed but will do nothing!
in the event log of the task scheduler, which is different than the usual application/system event logs, somewhere in there you will see a message in the lines of  [ action "C:\Windows\SYSTEM32\cmd.exe" with return code 1"]

Even looking at the task scheduler history would say that the task was executed and finished successfully!!!

The solution, add these 2 flag at the end of the command:

schtasks /Create /S [machine name] /RU [domain user] /RP [password] /SC ONCE /TN [task name] /TR [batch name i.e. DoSomething.bat] /ST [StartTime] /SD [RunDate] /RL HIGHEST  /F


If you are working on the server and scheduling the tasks using the GUI, here's a good link with screen shots of where these flags are hiding: WIN 2008 TASK SCHEDULER WITH RETURN CODE 1 (0X1)

Wednesday, March 14, 2012

SVN Upgrade Project Error ("Insufficient NODES rows for...)

In the last upgrade of our SVN from 1.6 to 1.7 I got the following error trying to upgrade the source folders on my machine using the VisualSVN "Upgrade Working Copy" menu command:

---------------------------

TortoiseSVN
---------------------------
Insufficient NODES rows for 'C:\[YOUR SOURCE PATH HERE]'
Try a 'Cleanup'. If that doesn't work you need to do a fresh checkout.
---------------------------
OK
---------------------------

to resolve the issue, search your source folder for files named [dir-prop-revert] they will be very small, and only contain a single line with the word "END" in it.
Delete these files and try the menu command again. it should do the trick.
 
*** make sure your search criteria includes hidden files ***

Thursday, January 12, 2012

Elegant solution for splitting a Comma Delimited string in SQL

came across this elegant simple solution for splitting a Comma Delimited string in SQL.
This will only work with SQL 2005 and up but it's so simple and elegant i had to share it.


There can be different types of Delimiters also : like Comma ' , ', vertical bar ' | ', Single space or a Tab.
For example here is our Sample Table -

Id

AllNames

1

A,B,C

2

A,B

3

X,Y,Z

And here is the expected output -

Id

Names

1

A

1

B

1

C

2

A

2

B

3

X

3

Y

3

Z
Create Sample Data :

-- Create Table for  Sample Data 

CREATE TABLE Test 

( 

ID INT, 

AllNames VARCHAR(100) 

) 

GO 

-- Load Sample Data 

INSERT INTO test SELECT 

1, 'A,B,C' UNION
ALL SELECT
 

2, 'A,B'  UNION
ALL SELECT
 

3, 'X,Y,Z' 

GO

-- Verify the Sample Data 

SELECT Id,
AllNames 

FROM Test
And here is the query for How to split a comma
delimited string :

;WITH Cte AS
( 

    SELECT 

        id, 

        CAST('<M>'
+ REPLACE(
Allnames,  ',' , '</M><M>') + '</M>' AS XML) AS Names 

    FROM Test 

) 

SELECT 

    ID, 

    Split.a.value('.', 'VARCHAR(100)') AS Names 

FROM Cte 

CROSS APPLY Names.nodes('/M') Split(a)





---------------------------------------------------------
Acknowledgement : SQL WITH MANGAL