This article revolves around being plain lazy. When it comes to creating Form code based on some database table, I hate it! This code sample goes along way in eeding this proce up for me. There still is some manual parts to finish up the form code but this takes care of remembering what colum are in the database table. In future releases, we'll provide more functionality to further automate this but this is a big first step in my opinion! The following four ste listed below can be followed and this will generate the A .NET code. A big thanks to Dave W. Webmaster of for saving me on many things!!
Define what database you want to co ect to in the config.web. This is stored in the co ection string
<configuratio gt;
lt;a etting gt;
lt;add key="d quot; value=" erver=localhost;uid=sa wd=;database=a free" />
lt;/a etting gt;
</configuratio gt;
Load the a x page in your browser, select the table to create the Form code from
Select the checkboxs of which fields to be on the form
Copy and paste into your code..
Here is a screen shot of the File after following the above ste .
Here is the code:
<%@ Page Language="V quot; EnableSe io tate="False" EnableViewState="True" Trace="False" Debug="False" Strict="True" %>
<%@ Import Name ace=" ystem.Text" %>
<%@ Import Name ace=" ystem.Data" %>
<%@ Import Name ace=" ystem.Data.SqlClient" %>
< cript language="V quot; runat=" erver" gt;
Dim sqlText as String
Dim ds as New DataSet
Dim dbComm AS New SQLDataAdapter
Dim co AS SQLCo ection
Dim sqlServer as String
Sub Page_Load(sender As Object, e As EventArgs)
QLserver = GetSqlCo ()
co = New SQLCo ection(SQLserver)
If Not I ostBack then
qlText = " elect id, name from sysobjects where xtype='U' order by name"
dbComm = New SQLDataAdapter(sqlText,co )
dbComm.Fill(ds,"AllTable quot;)
tblList.DataSource = ds.Tables("AllTable quot;).DefaultView
tblList.DataTextField = " ame"
tblList.DataValueField = " ame"
tblList.DataBind()
End if
End Sub
Function CreateValidator(myName as string) as String
Dim my as StringBuilder = New StringBuilder()
REM -- use :< ome text>: as placeholders
my .A end (" lt;a :RequiredFieldValidator runat=" quot erver" quot; id=" quot;:Name:" quot; ControlToValidate=" quot;:control:" quot; ErrorMe age=" quot;:errMsg:" quot; di lay=" quot tatic" quot gt;This Required Field!</a :RequiredFieldValidator> quot; )
my .Replace(":Name:","vld" & myName) 'add the validator name
my .Replace(":control:","at" & myName) 'add the control name
my .Replace(":errMsg:",myName & " is required")
Return my .ToString()
End Function
Function GetSqlCo () as String
Dim D as String = Configuratio ettings.A ettings("D quot;)
Return D
End Function
Sub GetTable_Click(sender As Object, e As EventArgs)
Dim sqlText as String
qlText = " ELECT syscolum .name, syscolum .i ullable FROM sysobjects I ER JOIN syscolum ON sysobjects.id = syscolum .id where sysobjects.name = '" & tblList.SelectedItem.Text & "' ORDER BY syscolum .colid"
REM -- Co ect to SQL
dbComm = New SQLDataAdapter(sqlText,co )
REM -- Fill DataSet
dbComm.Fill(ds,"TestData")
MyDataGrid.DataSource = ds.Tables("TestData").DefaultView
MyDataGrid.DataBind()
REM -- Show the results
myPanel.Visible= True
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
Dim i As Integer
Dim _item As DataGridItem
Dim dr As DataRow
Dim as StringBuilder = New StringBuilder()
Dim strOutput as String
REM -- Auto Generate The Form
.A end( " lt;form runat=" quot erver" quot; id=" quot;form2" quot; name=" quot;form2" quot gt quot; & Chr(13) & Chr(10) )
.A end( " <table border=1> quot; & chr(13))
For i = 0 To MyDataGrid.Items.Count - 1
REM -- Get the checkbox
_item = MyDataGrid.Items(i)
Dim addCheckBox As CheckBox = Ctype(_item.FindControl("chkAdd"),CheckBox)
Dim validCheckBox as CheckBox = Ctype(_item.FindControl("chkValid"),CheckBox)
If addCheckBox.Checked then
.A end( " <tr> quot; & chr(13))
.A end( " <td> quot; & _item.Cells(1).Text & " lt;/td> quot; & chr(13))


