日期:2011-02-21  浏览次数:20510 次

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 speeding this process up for me.   There still is some manual parts to finish up the form code but this takes care of remembering what columns 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 steps listed below can be followed and this will generate the ASP.NET code. A big thanks to Dave W. Webmaster of  for saving me on many things!!

Define what database you want to connect to in the config.web.  This is stored in the connection string
<configuration>
    <appsettings>
        <add key="dsn" value="server=localhost;uid=sa;pwd=;database=aspfree" />
    </appsettings>
</configuration>
Load the aspx 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 steps.



Here is the code:

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="True" Trace="False" Debug="False" Strict="True" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat="server">
Dim sqlText as String
Dim ds as New DataSet
Dim dbComm AS New SQLDataAdapter
Dim conn AS SQLConnection
Dim sqlServer as String

Sub Page_Load(sender As Object, e As EventArgs)

          SQLserver = GetSqlConn()
          conn = New SQLConnection(SQLserver)


          If Not IsPostBack then
                    sqlText = "select id, name from sysobjects where xtype='U' order by name"
                    dbComm = New SQLDataAdapter(sqlText,conn)
          dbComm.Fill(ds,"AllTables")
          tblList.DataSource = ds.Tables("AllTables").DefaultView
          tblList.DataTextField = "name"
          tblList.DataValueField = "name"
          tblList.DataBind()
End if


End Sub

Function CreateValidator(myName as string) as String
          Dim mySB as StringBuilder = New StringBuilder()

          REM -- use :<some text>: as placeholders
          mySB.Append ("<asp:RequiredFieldValidator runat=""server"" id="":Name:"" ControlToValidate="":control:"" ErrorMessage="":errMsg:"" display=""Static"">This Required Field!</asp:RequiredFieldValidator>" )

          mySb.Replace(":Name:","vld" & myName) 'add the validator name
          mySb.Replace(":control:","at" & myName) 'add the control name
         &n