ASP.NET Code
  Home arrow ASP.NET Code arrow Form code generator V1.1 by Steve Schofiel...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

Form code generator V1.1 by Steve Schofield
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2001-07-20

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!


    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. for saving me on many things!!
    1. 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>
    2. Load the aspx page in your browser, select the table to create the Form code from
    3. Select the checkboxs of which fields to be on the form
    4. Copy and paste into your code..
    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)

    If addCheckBox.Checked then
    sb.Append( " <tr>" & chr(13))
    sb.Append( " <td>" & _item.Cells(1).Text & "</td>" & chr(13))
    sb.Append( " <td>")
    sb.Append( "<asp:textbox id=""at" & _item.Cells(1).Text & """ runat=""server"" />" )

    'create a validator control
    If validCheckBox.Checked then
    sb.Append (" " & chr(13) & CreateValidator(_item.Cells(1).Text ) )
    End if

    sb.Append ("</td>" & chr(13)) '
    sb.Append ( " </tr>" & chr(13)) ' close out the row
    End if

    Next
    sb.Append ( " <tr>" & chr(13)) ' close out the row
    sb.Append(" <td colspan=""2""><asp:button id=""button1"" Text=""Validate Form"" runat=""Server"" /></td>" & chr(13))
    sb.Append ( " </tr>" & chr(13)) ' close out the row
    sb.Append ( " </table>" & chr(13) )
    sb.Append(chr(13) & "</form>")
    strOutput = sb.ToString()
    strOutput = System.Web.HttpUtility.HTMLEncode(strOutput)
    taResults.Value = strOutput
    pnlTextarea.Visible=True
    End Sub

    </script>

    <html>
    <head>
    <title></title>
    </head>
    <body bgcolor="#FFFFFF" >
    <form runat="server" id=form1>
    Select a tablename to create a .NET form for:
    <asp:dropdownlist id="tblList" runat="server"/>&nbsp;&nbsp;
    <asp:Button id=GetTable Text="Get Table" onclick="GetTable_Click" runat="server" />

    <asp:panel id=myPanel runat="server" visible="false">
    <br>
    Select the Columns used for generating the form.

    <asp:datagrid id=MyDataGrid runat="server"
    BorderColor="black"
    BorderWidth="1"
    CellPadding="3"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    AutoGenerateColumns="False"
    >
    <Columns>
    <asp:TemplateColumn HeaderText="Add?">
    <ItemTemplate>
    <center>
    <asp:CheckBox id=chkAdd runat="server" />
    </center>
    </Itemtemplate>
    </asp:TemplateColumn>

    <asp:BoundColumn HeaderText="Name" DataField="name"/>

    <asp:TemplateColumn HeaderText="Create Validator?">
    <ItemTemplate>
    <center>
    <asp:CheckBox id=chkValid runat="server" />
    </center>
    </Itemtemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    <asp:Button id=Button1 Text="Create Form" onclick="Button1_Click" runat="server" />
    </asp:panel>

    <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>

    </form>
    </body>
    </html>

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP.NET Code Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    Be the first to hear about i5/OS V6R1!

    Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br />
    FREE! Go There Now!


    NEW! Application development for the OLPC laptop

    The XO laptop (of the One-Laptop-Per-Child initiative) is an inexpensive laptop project intended to help educate children around the world. The XO laptop includes many innovations, such as a novel, inexpensive, and durable hardware design and the use of GNU/Linux as the underlying operating system. The XO also includes an application environment written in Python with a human interface called Sugar, accessible to everyone (including kids). Explore the Sugar APIs and learn how to develop and debug a graphical activity in Sugar using Python.
    FREE! Go There Now!


    NEW! Application Development Tools for the Mainframe Developer

    You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months.
    FREE! Go There Now!


    NEW! Cook up Web sites fast with CakePHP, Part 1: Adding related information and services

    CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP.
    FREE! Go There Now!


    NEW! Did you say mainframe? e-kit

    Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise.
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Tester for SOA Quality V7.0.1

    Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services.
    FREE! Go There Now!


    NEW! Web development with Eclipse Europa, Part 1: The Java EE for Eclipse

    It's a good time to be a Web developer. You've never had more choices in terms of technologies. There are so many great open source Web servers, databases, programming languages, and development frameworks. No matter what combination of technologies you prefer to work with, there is an integrated development environment (IDE) that can increase your productivity: Eclipse. In this tutorial, Part 1 of a three-part "Web development with Eclipse Europa" series on how to use Eclipse for Web development with Java technology, PHP, and Ruby, we'll see how the latest release of Eclipse -- Europa -- can be used to rapidly develop Java Web applications. We'll use Java Platform, Enterprise Edition 5 (Java EE) for Eclipse to build a Web application for tracking and calculating baseball statistics.
    FREE! Go There Now!


    NEW! Web development with Eclipse Europa, Part 3: Ruby Development Toolkit and RadRails

    It's a good time to be a Web developer. You've never had more choices in terms of technologies. There are so many great open source Web servers, databases, programming languages, and development frameworks. No matter what combination of technologies you prefer to work with, there is a single integrated development environment (IDE) that can increase your productivity: Eclipse. Here in Part 3, we introduce the RDT and RadRails Eclipse plug-ins and show you how to get these plug-ins and start using them. You will learn how to use RadRails to do many common Ruby on Rails development tasks.
    FREE! Go There Now!


    NEW! Webcast: Quickly provide customized, integrated user interfaces with Lotus Notes 8

    IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway