T-SQL Form Generator Template

Most of the time web developer spends creating some kind of forms. This is why almost any object-relational mapper product (EntitySpaces, Subsonic, LINQ to SQL, NHibernate) has some kind of scaffolding generators. Generated pages are might be good for admin side, but for front-end those pages needed to be created manually. Front-end usually has specific design requirements, so there you need to do this tedious task manually over and over again.

You can always use T4 templates as code generator, but I prefer to use old solid T-SQL.

Microsoft SQL Server Management Studio has a lot of pre-build templates for you just press Ctrl+ALT+T to see them (View->Template Explorer). If you are not using them yet, check them out it might be really helpful. Hint: Ctrl+Shift+M brings parameter menu.

Let's create simple T-SQL template form generator for ASP.NET:

Declare @width varchar(3)

set @width = '<field_width,int,150>'

 

select

'<tr>

    <td class="textfield">' + COLUMN_NAME + '</td>

    <td class="valuefield"><asp:TextBox ID="txt' + COLUMN_NAME + '" runat="server" Width="' + @width +'"px"></asp:TextBox></td>

</tr>'

from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '<table_name,sysname,Table Name>'


<field_width,int,150> - width of input box template parameter
<table_name,sysname,Table Name> - table name template parameter
Press Ctrl+Shift+M. Set above parameters. Switch to "Result to Text" mode. Run and your form HTML is ready. Just copy paste it in your aspx file.

This is a good start. But forms are rarely consist of just TextBox-es, so lets modify this a little bit:

select

case

when c.CONSTRAINT_TYPE = 'PRIMARY KEY' then

'<tr>

    <td class="textfield">' + cl.COLUMN_NAME + ':</td>

    <td class="valuefield"><asp:Literal ID="lit' + cl.COLUMN_NAME + '" runat="server" /></td>

</tr>'

when DATA_TYPE = 'bit' then

'<tr>

    <td class="textfield">' + cl.COLUMN_NAME + ':</td>

    <td class="valuefield"><asp:CheckBox ID="chk' + cl.COLUMN_NAME + '" runat="server" /></td>

</tr>'

when DATA_TYPE = 'varchar' and CHARACTER_MAXIMUM_LENGTH = -1 then

'<tr>

    <td class="textfield">' + cl.COLUMN_NAME + ':</td>

    <td class="valuefield"><asp:TextBox ID="txt' + cl.COLUMN_NAME + '" TextMode="MultiLine" runat="server" Width="' + @width +'px"></asp:TextBox></td>

</tr>'

when DATA_TYPE = 'date' then

'<tr>

    <td class="textfield">' + cl.COLUMN_NAME + ':</td>

    <td class="valuefield"><dxe:ASPxDateEdit ID="txt' + cl.COLUMN_NAME + '" runat="server" Width="' + @width +'px"></dxe:ASPxDateEdit></td>

</tr>'

else

'<tr>

    <td class="textfield">' + cl.COLUMN_NAME + ':</td>

    <td class="valuefield"><asp:TextBox ID="txt' + cl.COLUMN_NAME + '" runat="server" Width="' + @width +'px"></asp:TextBox></td>

</tr>'

end

from

INFORMATION_SCHEMA.COLUMNS cl LEFT JOIN

INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u on cl.COLUMN_NAME = u.COLUMN_NAME and cl.TABLE_NAME = u.TABLE_NAME  LEFT JOIN

INFORMATION_SCHEMA.TABLE_CONSTRAINTS c on u.CONSTRAINT_NAME = c.CONSTRAINT_NAME AND c.TABLE_NAME = cl.TABLE_NAME

where cl.TABLE_NAME = '<table_name,sysname,Table Name>'


I hope this helps. I used some DevExpress controls in my code, which might not be suitable for your. Feel free to modify template according to your needs.


kick it on DotNetKicks.com
Monday, January 25, 2010 | Add Comment

How to make Visual Studio toolbox icon have transparent background?

During my web developer career I wrote about a dozen of ASP.NET server controls. However transparent background in Visual Studio toolbox was always a mystery for me. Because of that I usually used icons without transparent background. I noticed that Devexpress controls has those kind of icons, so I used reflector to browse their resources and investigated this issue. To make long story short, here are icon with transparent background requirements:

Image Format

If you want transparent background you need to use 16x16 8-bit BMP. You can use other types of images jpeg or png, but transparent background works only with BMP. This is very strange since BMP doesn't have alpha channel, so no build-in transparency.

Transparency Pixel

Select neutral to your icon color and color with it all transparent places. Devexpress usually uses Magenta (#ff00ff) for this purposes. I order to make selected color transparent you need to color bottom left corner pixel of your icon with it.

Wednesday, January 20, 2010 | Add Comment

How to query Active Directory from MS SQL Server?

Recently, I am creating intranet portal for a company that I am working for. Active directory has a lot of build-in fields for users like first name, last name, email, phone. But it still not enough for a bussiness needs. So, I had to create additional table for extended information in ms sql server database. Now how do you connect extended information and information from active directory?

Active directory has objectGuid field. You can use this field to connect your table and active directory information. .NET has two ways to access active directory data using old ODBC method or System.DirectoryServices namespace. However manual connection of table from sql server and active directory information is too much of work. It is much more easier to use linked server feature of MS SQL Server.

sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces', 'ADSDSOObject', 'adsdatasource'

And then you can query linked server like this:

SELECT * FROM OpenQuery(ADSI, 'SELECT title, displayName, sAMAccountName, givenName, telephoneNumber, facsimileTelephoneNumber, sn FROM ''LDAP://DC=whaever,DC=domain,DC=org'' where objectClass = ''User''')


You might see following error:

The OLE DB provider "ADSDSOObject" for linked server "ADSI" reported an error. The provider indicates that the user did not have the permission to perform the operation.

Then you need to go to properties of newly created linked server and adjust security settings (Server Objects > Linked Servers > ADSI).

Monday, January 11, 2010 | Add Comment