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 mysb.replace(":errmsg:",myname & " is required")
return mysb.tostring()
end function
function getsqlconn() as string dim dsn as string = configurationsettings.appsettings("dsn") return dsn end function
sub gettable_click(sender as object, e as eventargs) dim sqltext as string sqltext = "select syscolumns.name, syscolumns.isnullable from sysobjects inner join syscolumns on sysobjects.id = syscolumns.id where sysobjects.name = '" & tbllist.selecteditem.text & "' order by syscolumns.colid"
rem -- connect to sql dbcomm = new sqldataadapter(sqltext,conn)
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 sb as stringbuilder = new stringbuilder() dim stroutput as string
rem -- auto generate the form sb.append( "<form runat=""server"" id=""form2"" name=""form2"">" & chr(13) & chr(10) ) sb.append( " <table border=1>" & 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)
<asp:panel id="pnltextarea" visible="false" runat="server"> <p>copy this code into a new asp.net page</p> <textarea id=taresults cols=90 rows=40 runat="server" /> </asp:panel>