Tuesday, December 6, 2011

Regular Expression for SQL Login Name / Database User Name

I am no expert in regular expressions but I had to work on a expression to validate sql login name and database user name to avoid sql injection as the login and database user creation proc uses dynamic sql.


^([A-Za-z0-9\._]*[\\]?[A-Za-z0-9\._]*)|((?!([A-Za-z0-9-\._])*\-\-)([A-Za-z0-9-\._])*)$


Satisfies the following condition:
letters, numbers(alpha numeric), a single backslash (\), hyphens (-), underscores (_) and periods (.) no more than one consecutive hyphen.


You can use the expression above in the .net code to do the validation in the UI.


This is the proc that acheives the same result:


IF OBJECT_ID('dbo.uspCreateLogin', 'P') IS NOT NULL
 DROP PROCEDURE dbo.uspCreateLogin
go

CREATE
PROCEDURE dbo.uspCreateLogin

(
@LoginName varchar(128)

 )
AS

SET NOCOUNT ON;


 DECLARE
@Index INT,
 @SQL VARCHAR(500),
 @Invalid BIT;

--No two slashes in the login name
 IF LEN(@LoginName) - LEN(REPLACE(@LoginName, '\', '')) > 1
BEGIN
 SET @Invalid = 1;
 END;


--No two consecutive hypens in the name
 IF @Invalid = 0 AND LEN(@LoginName) - LEN(REPLACE(@LoginName, '--', '')) > 0
BEGIN
 SET @Invalid = 1;
 END;


--Only letters, numbers, backslash (\), hyphens (-), underscores (_) and periods (.)
 IF @Invalid = 0
BEGIN
 SET @Index = PATINDEX('%[^0-9A-Za-z-\._]%', @LoginName)

IF @Index > 0
BEGIN
 SET @Invalid = 1;
 END;
 END


IF @Invalid = 1
BEGIN

RAISERROR ('only letters, numbers, a single backslash (\), hyphens (-), underscores (_) and periods (.) and no consecutive hyphens.', 11, 1);

RETURN 1;

END;

-- Check that the login doesn't already exist

IF EXISTS (
 SELECT 1
FROM sys.SysLogins
 WHERE LoginName = @LoginName
)

BEGIN
 RAISERROR ('The login already exists.', 11, 1);
 RETURN 1;
 END;

SET @SQL = 'CREATE LOGIN ' + QUOTENAME(@LoginName) + ' FROM WINDOWS;';


EXEC(@SQL);
 GO


Keywords
TSQL Regular Expression
CREATE LOGIN
CREATE USER
SQL SERVER

Tuesday, November 29, 2011

New Values Clause Sql Server

Values clause in sql server is a new way to insert mulitple rows into the table one time.This is how to do it.


DECLARE @InputTable table (
  Name varchar(max),
 ID uniqueidentifier
)

INSERT INTO @InputTable
VALUES
('a','487671D5-1F05-4F2D-9A3B-5DC7EA3E046E'),
('b','487671D5-1F05-4F2D-9A3B-5DC7EA3E046E'),
('c','487671D5-1F05-4F2D-9A3B-5DC7EA3E046E')

selectfrom @InputTable;

------------------
Name ID
a 487671D5-1F05-4F2D-9A3B-5DC7EA3E046E
b 487671D5-1F05-4F2D-9A3B-5DC7EA3E046E
c 487671D5-1F05-4F2D-9A3B-5DC7EA3E046E

Tuesday, September 13, 2011

XPS M1530 NO DISPLAY / VIDEO BUT LIGHTS ON - Fixed

OK, I don't know who said Women are better at baking than men.Being a man I don't know if I want to agree on that after baking the motherboard of dell xps m1530.

At times you have to see things you want to beleive especially when there are so many people faking many things on the web, baking a motherboard in oven is one of those.

After 2 years of purchase of the dell xps m1530, it started to show black screen at me when I turned on the machine.Lights come on and cd/dvd drive seems to spin for a second but no display, connecting to the external display did not help either.

Did lot of searching on the Internet and many people out there have the same problem like me.It seems that the problem is coming from the poor design of the motherboard with a faulty nvidia graphics chip. Surprisingly people were saying heating the graphics chip area with a hair blower seems to fix the problem for a while. I cleaned the fan, removed the old thermal paste and put the new one on but still the problem existed.

Didn't give up, started searching on YouTube and found reflow seems to fix the problem.I read more about it and found lot of info on how to do it at home using a oven.

To do this I had to take the motherboard out of the laptop and remove all the stickers/labels on the motherboard so they don't burn when baking.Baked the motherboard at 356 F for about 8 Minutes and cooled it for about an hour before I put everything together.DO NOT move the motherboard after baking it, let it cool for while before moving it as it might cause the components fall off or move.

I put it together and it all seems working since then.

If you are trying this at home, you are doing it ON YOUR OWN RISK.











Thursday, August 18, 2011

Simple Nant Build file - revert switch update

Nant you might all know is a powerful tool when creting builds.It offers lot of flexibility when it come to building projects.Nant along with subversion and wix can make your life easier to build your app.Here is a simple post that shows you how to use nant and get files.

Following script does
1.Clenup
2.Switch
3.remove unversioned files and directories
 4.revert
5.update

<?xml version="1.0" encoding="utf-8"?>
<project name="testscriptfile" default="build" basedir=".">

<property name="subversion.username" value="YourUserName" />
<property name="subversion.password" value="YourPassWord" />

<property name="subversion.projectpath" value="YoutPath" />
<property name="dir.project" value="yourPhysicalFilesPath" />

<property name="subversion.revision" value="-1" />
<property name="subversion.projectpathwithrevision" value="" />

<target name="setdependencyproperties">
<property name="subversion.projectpathwithrevision" value="${subversion.projectpath}" />
<if test ="${subversion.revision > '-1'}">
<property name="subversion.projectpathwithrevision" value="${subversion.projectpath} -r ${subversion.revision}" />
</if>
</target>


<target name="build" description="gets the latest files from subversion" depends ="setdependencyproperties">

<echo message="#######################################" />
<echo message="CLEANING : ${dir.project}" />
<echo message="#######################################" />
<exec program="svn" commandline="cleanup ${dir.project}" />
<echo message=" " />


<echo message="#######################################" />
<echo message="REVERTING : ${dir.project}" />
<echo message="#######################################" />
<exec program="svn" commandline="revert --recursive ${dir.project}" />
<echo message=" " />

<echo message="#######################################" />
<echo message="REMOVING UNVERSIONED FILES/DIRS FROM : ${dir.project}" />
<echo message="#######################################" />

<exec program="cmd">
<arg value="/k"/>
<arg value="for /f &quot;tokens=1*&quot; %i in ('svn status ${dir.project} ^| find &quot;?&quot;') do del /q &quot;%j&quot;"/>
</exec>

<exec program="cmd">
<arg value="/k"/>
<arg value="for /f &quot;tokens=1*&quot; %i in ('svn status ${dir.project} ^| find &quot;?&quot;') do rd /s /q &quot;%j&quot;"/>
</exec>

<echo message=" " />

<echo message="#######################################" />
<echo message="SWITCHING : ${subversion.projectpathwithrevision}" />
<echo message="#######################################" />
<exec program="svn" commandline="switch ${subversion.projectpathwithrevision} ${dir.project} " />
<echo message=" " />

<echo message="#######################################" />
<echo message="GETTING FILES FROM : ${subversion.projectpathwithrevision} TO ${dir.project}" />
<echo message="#######################################" />
<exec program="svn" commandline="update --username ${subversion.username} --password ${subversion.password} ${subversion.projectpathwithrevision} ${dir.project}" />

</target>
</project>

StatusStrip missing windows vb.net c# mdi windows forms application


All of a sudden the status bar in our MDI windows forms application is lost when you resize the form smaller.

Here is the scenrario

MDI Parent -  Has a StatusStrip that contains 3 ToolStripLabels that has spring property set to false.

MDI Child - Has a StatusStrip that contains 1 ToolStripLabel that has spring property set to true.MDI Child form size is 1280 x 1024 (design time).

In the mdi child activate event of the mdi parent , the statusstrip from the mdi child is merged into the statusstrip of the mdi parent using the ToolStripManager.Merge method.

It seems the problem is "toolstriplabel" in mdi child spring is set to true and the width of the toolstriplablel is close to 1280(form width).So when you resize your mdi parent smaller than the design width of the toolstriplabel is trying to steal the 1280 width, as it is not available it went some where, also it is pushing the labels in the mdi area go out of the visible area.

To fix this problem ;
1.make the toolstriplabel spring to false and make it smaller(min width that your form can go)
2.merge the statusstrips
3.set the spring property of the toolstrip label(from the mdi child) in the mdi parent after you do the merge to TRUE.

Hope this helps some one out there.!!!

 


Thursday, April 28, 2011

Wix Build Error - Nant

error MSB4019: The imported project "C:\Program Files\MSBuild\Microsoft\WiX\v3.x\Wix.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

Update your wix projects to newer version when you upgrade wix on the machine.

Thursday, April 14, 2011

Context 0x1b64e0' is disconnected : DirectoryEntry

I started getting the exception below everytime I close my vb.net application.To fix this I had to either dispose the directory entry object after calling the ProcessDirectoryEntry in the for loop OR change "byval" to "byref" in the ProcessDirectoryEntry de parameter.

 For Each de As DirectoryEntry In delist
            ProcessDirectoryEntry(de)
    ''fix - de.dispose
Next
Sub ProcessDirectoryEntry(ByVal de As DirectoryEntry)
        ..
            ..
            ..
            ..
End Sub


Context 0x1b64e0' is disconnected.  Releasing the interfaces from the current context (context 0x1b6370). This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

Monday, April 4, 2011

SQL Profiler and Management Studio batch files for launching

SQL Profiler
Sometimes you want to run your profiler as a different user than the user that is logged onto the machine and uses a template of your choice then the following lines in a batch file can help you.

SET SSMSEXE=C:\Program Files\Microsoft SQL Server\100\Tools\Binn\PROFILER.EXE
C:\WINDOWS\system32\runas.exe /user:Domain\otheruser /savecred "%SSMSEXE% /S spa /T mytemplate"

--
Launching Management Studio with Domain\OtherUser,windows authentication and dv_dev as default database.

SET SSMSEXE=C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe
C:\WINDOWS\system32\runas.exe /user:Domain\OtherUser /savecred "%SSMSEXE% -s spa -d DB_Dev -E -nosplash"
Tested under SQL Server 2008R2 only.

AT&T DSL Setup

OK, I switched from Uverse to DSL and opted for getting my own modem.But what I didn't realize is that only some modems are supported by AT&T.I bought Zyxel Prestige 600 Series 645 and tried to make it work with AT&T with no luck.Tried to logon to the device using telnet and set the user name and password,vci,vpi,multicasting and encapsulation.All the settings seems ok, I can see the modem stats but couldn't connect to at&t.

After days of struggling with the help of at&t representatives, I gave up and got SpeedStream 5100 modem along with Linksys WRT54g and was able to get on to the internet.Make sure you set the router to ppoe and set the username and password, plug the ethernet cable from the speedstream modem into the wan port and you should be able to get to the internet.