How to Disable The Cross X Button in Winform

I had this requirement to to not allow user to close a windows form by clicking the top-right cross button.  This can be done by checking the FormClosing Event.

 

private void DataCheckInspection_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
    }
}

TSQL-How to Contract Numbers

I was trying to contract numbers from “1” to “1st”, 2 to “2nd” etc.

This amazing solution from Brad Schulz at

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/6d176328-908c-4b00-8768-eb71b7c57e7b

did the job for me.

SELECT CAST(Number AS VARCHAR(10))
      +CASE 
         WHEN Number%10=1 AND Number%100<>11 THEN 'st'
         WHEN Number%10=2 AND Number%100<>12 THEN 'nd'
         WHEN Number%10=3 AND Number%100<>13 THEN 'rd'
         ELSE 'th' 
       END
FROM master..spt_values
WHERE type='P' 

TSQL– From Datetime to String in yyyy-mm-dd hh:mm:ss.mmm Format

I had this requirement to create some dynamic SQL script for a client.  I had a data source with dates in UK format.  The script had to have dates in datetime’s normal format.  I was creating the script from the data source.  Lets look at an example:

 

I had a varchar value ‘18/10/2012’ in the data source.  I had to put equivalent datetime value in the database.  So the dynamic script was required to have correct value.

Using CONVERT (DATETIME, value, 103) gave me the correct datetime format but using that for dynamic SQL was not possible as the returned result was DATETIME.

So I had to again CONVERT the DATETIME into VARCHAR with option 121.  Here is an example:

 

 

2012-10-24_1037

T-Mobile, Orange and Everything Everywhere 3G Problem

Last morning I received an SMS from T-mobile (my current provider) stating that if I restarted my phone set I would see EE instead of T-mobile as a signal provider.  I did the restart and was able to see EE instead of T-Mobile at the top of the screen.  All fine and well.  Problem started when I wanted to make a phone call.  I was getting disconnected right away.  I went to the nearest T-mobile centre (Barnet Spires Mall), and the very helpful lady there was able to solve the issue.  Apparently Everything-Everywhere is having problem with its 3G network because of 4G network work.  The lady set my phone to get the 3G network from a manually selected list (when searching for available list two EE options came, she selected the bottom one).  The phone started working.  As I normally use the mobile only for phone calls I asked her is it better to change signal to 2G (GMS instead of UMTS).  She said that was also worth a try.  I changed my signal type to 2G.  This was also working fine.  During this rollover period for 4G I think this problem will go on for some time.

T-Mobile, Orange and Everything Everywhere 3G Problem

Last morning I received an SMS from T-mobile (my current provider) stating that if I restarted my phone set I would see EE instead of T-mobile as a signal provider.  I did the restart and was able to see EE instead of T-Mobile at the top of the screen.  All fine and well.  Problem started when I wanted to make a phone call.  I was getting disconnected right away.  I went to the nearest T-mobile centre (Barnet Spires Mall), and the very helpful lady there was able to solve the issue.  Apparently Everything-Everywhere is having problem with its 3G network because of 4G network work.  The lady set my phone to get the 3G network from a manually selected list (when searching for available list two EE options came, she selected the bottom one).  The phone started working.  As I normally use the mobile only for phone calls I asked her is it better to change signal to 2G (GMS instead of UMTS).  She said that was also worth a try.  I changed my signal type to 2G.  This was also working fine.  During this rollover period for 4G I think this problem will go on for some time.

Reset SA password for SQL box

My old box was changed to different domain and I could not use Windows authetication to log into the SQL server there as “sysadmin”. I forgot the SA user password as well. After some search I found the following solutions

Option 1 :

If there is any other SQL Server Login that is a member of sysadmin role, you can log in using that account and reset the password of SQL Server.

Option 2 :

If there is any other Windows Login that is a member of Windows Admin Group, log in using that account. Start SQL Server in Single User Mode as described here : SQL SERVER – Start SQL Server Instance in Single User Mode.

Create a new login and give it sysadmin permission.

Note : If you have SQL Server Agent enabled, it starts before SQL Server service. If you have enabled SQL Server in a single user mode, it will connect it first, so it is recommended to turn that off before attempting any of the above options.

I used the second option and was able to reset SA’s password.

Start SQL server in “single user” mode.

To start SQL Server in single user mode is very simple procedure as displayed below.

Go to SQL Server Configuration Manager and click on SQL Server 2005 Services. Click on desired SQL Server instance and right click go to properties. On the Advance table enter param ‘-m;‘ before existing params in Startup Parameters box.

clip_image001

Make sure that you entered semi-comma after -m. Once that is completed, restart SQL Server services to take this in effect. Once this is done, now you will be only able to connect SQL Server using sqlcmd.

Make sure to remove newly added params after required work is completed to restart it in multi user mode.

Now I was able to login to the SQL server with windows authentication.

I change the password for user SA.

Thanks to links from:

SQL SERVER – Forgot the Password of Username SA

SQL SERVER – Start SQL Server Instance in Single User Mode

Have Multiline Strings in VS

I frequently face situation where a string variable has a long length, so long that I have to scroll the screen left to right to see it.  What helps in this situation is to have breaks in that string so that the full string can be seen without having to scroll left-right.

Using “@” before the string’s value helps in this case.  For example,

string mysql = "select table1.col1,table1.col2,table1.col3, table2.column3, table2.col4, table2.col4 from table1 join table2 on table1.col1 = table2.col2"

can be written as:

string mysql = @"select table1.col1,table1.col2,table1.col3
, table2.column3, table2.col4, table2.col4
from table1 join table2
on table1.col1 = table2.col2"