RSS 2.0
 Tuesday, September 18, 2007

Call it a code generator, software factory, or just a clever script. If you can write code that writes code - you win, even if just a small victory for humans in this contest we call software development.

For example, I've been working on an ASP.NET application which contains many data entry screens. You know the kind: very simple, table-format with a label and a textbox of a certain width, that may or may not require some validation. In other words, a whole lot of markup like this:

<tr>

  <td class="EditLabel">Number</td>

  <td class="Edit">

    <asp:TextBox ID="txtNumber" runat="Server" Width="200px" MaxLength="20"></asp:TextBox>

  </td>

</tr>

 

Now, if you have to type the above more than once or twice, you will go insane (been there, gone there). More importantly, you will probably introduce a bug or two. So, I opened up SQL Server 2005 Management Studio and wrote the following T-SQL code:

USE SomeDB

GO

 

DECLARE @Table  varchar(128)

DECLARE @Column varchar(128)

DECLARE @Width  varchar(10)

DECLARE @Length int

DECLARE @Type   int

 

SET @Table = 'Employer' -- Pass this as a parameter

 

DECLARE ColumnCursor CURSOR FOR

   SELECT C.Name, C.Max_Length, C.User_Type_ID FROM Sys.Columns C

   INNER JOIN Sys.Tables T ON C.Object_ID = T.Object_ID

   WHERE T.Name = @Table

   ORDER BY Column_ID

 

OPEN ColumnCursor

 

FETCH NEXT FROM ColumnCursor INTO @Column, @Length, @Type

WHILE @@FETCH_STATUS = 0

BEGIN

  IF @Type <> 36 -- No GUIDs

  BEGIN

    IF @Length < 0 SET @Length = 100

    IF @Length < 10

      SET @Width = '50px'

    ELSE IF @Length < 20

      SET @Width = '100px'

    ELSE IF @Length < 50

      SET @Width = '200px'

    ELSE IF @Length < 100

      SET @Width = '300px'

    ELSE

      SET @Width = '400px'

 

    PRINT '<tr>'

    PRINT '  <td class="EditLabel">' + @Column + '</td>'

    PRINT '  <td class="Edit">'

    PRINT '    <asp:TextBox ID="txt' + @Column + '" runat="Server" Width="' + @Width + '" MaxLength="' + CONVERT(varchar(10),@Length) + '"></asp:TextBox>'

    PRINT '  </td>'

    PRINT '</tr>'

  END

  FETCH NEXT FROM ColumnCursor INTO @Column, @Length, @Type

END

 

CLOSE ColumnCursor

DEALLOCATE ColumnCursor

 

You get the picture. Feel free to customize this code to introduce additional formatting, a slick UI, or other business rules to the mix.

Tuesday, September 18, 2007 4:02:47 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [4] -
SQL Server | SQLblog
Tuesday, September 18, 2007 4:39:32 PM (Mountain Daylight Time, UTC-06:00)
I'm not suer if you are joking and just waiting for people to gasp at the sheer Rube Goldbergeskness of it all...
Or if you are serious and I will have to kill you next time I see you.

So now we can have a web page that calls a SQL Server stored procedure, which calls this method, which creates an asp.net page,...I think you can add some more steps into this. Maybe an XML Transformation or two.
Thursday, September 20, 2007 2:53:52 PM (Mountain Daylight Time, UTC-06:00)
I've been down this road. You're 100 times better of using Codesmith
Saturday, September 22, 2007 10:16:18 AM (Mountain Daylight Time, UTC-06:00)
Chris - no, my code doesn't call this - it's a design-time tool and yes, Codesmith is superior (bowing head, holding up dead-animal offering), but not everyone has it. :-)
Wednesday, September 26, 2007 9:42:29 PM (Mountain Daylight Time, UTC-06:00)
I still don't see why you should handle this in database and not at the UI. I assume that you are showing that it possible to be done that way too.
Chua Wen Ching
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Archive
<August 2008>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Richard Hundhausen
Sign In
Statistics
Total Posts: 690
This Year: 26
This Month: 0
This Week: 0
Comments: 479
Themes
Pick a theme:
All Content © 2008, Richard Hundhausen
DasBlog theme 'Business' created by Christoph De Baene (delarou)