Writing ASP/HTML insert, update and delete pages for every table in ever project
is a boring waste of time. So I am working on a "Data Form Wizard" for ASP.NET.
I started with the update form, because that is the hardest. I was amazed
at how little code it takes by leveraging the DataSet. Check it out, and
let me know what you think.

1.Place this UserControl named, "CtlUpdate.asx" in your project:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="CtlUpdate.ascx.cs"
Inherits="SalesApp1.CtlUpdate"%>

2.Place the Codebehind page in your project, here is the code:
namespace ControlDemo
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

/// <summary>
/// Summary description for CtlUpdate.
/// </summary>
public abstract class CtlUpdate : System.Web.UI.UserControl
{

protected Button bSubmit = new Button();
private string m_sqlSelect;
private System.Data.OleDb.OleDbConnection m_cn;
private System.Data.DataSet ds = null;
private OleDbDataAdapter da = null;

public CtlUpdate()
{
this.Init += new System.EventHandler(Page_Init);
this.Load += new System.EventHandler(Page_Load);
}

private void Page_Load(object sender, System.EventArgs e)
{
da = new OleDbDataAdapter(m_sqlSelect,m_cn);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(da);
ds = new DataSet();
da.Fill(ds);

Literal tmpLit = new Literal();

DataRow dr = ds.Tables[0].Rows[0];
if(dr != null)
{
tmpLit.Text = "<table border='0'>";
this.Parent.Controls.Add(tmpLit);

foreach(DataColumn dc in dr.Table.Columns)
{
tmpLit = new Literal();
tmpLit.Text = "<tr><td>" + dc.ColumnName +":</td><td>";
this.Parent.Controls.Add(tmpLit);

TextBox tmp = new TextBox();
tmp.Text = dr[dc.Ordinal].ToString();
tmp.ID =dc.ColumnName;
this.Parent.Controls.Add(tmp);

tmpLit = new Literal();
tmpLit.Text = "</td></tr>";
this.Parent.Controls.Add(tmpLit);
}
tmpLit = new Literal();
tmpLit.Text = "</table>";
this.Parent.Controls.Add(tmpLit);

bSubmit.Text="Update";
this.Parent.Controls.Add(bSubmit);
}
}

private void Page_Init(object sender, EventArgs e)
{
}

private void bSubmit_Click(object sender, System.EventArgs e)
{
DataRow dr = ds.Tables[0].Rows[0];
if(dr != null)
dr.BeginEdit();
foreach(DataColumn dc in dr.Table.Columns)
{
dr[dc.Ordinal] = ((TextBox)this.Parent.FindControl(dc.ColumnName)).Text;
Trace.Write("dc.Ordinal Complete",dc.Ordinal.ToString());
}
Trace.Write("test","update complete");
dr.EndEdit();
da.Update(ds);
ds.AcceptChanges();
}
public string sql
{
set
{
m_sqlSelect = value;
}
get
{
return m_sqlSelect;
}
}
public System.Data.OleDb.OleDbConnection connection
{
set
{
m_cn = value;
}
get
{
return m_cn;
}
}

}
}
3.In an aspx page place register the above user control and place control
tag in a form:
<%@ Page Trace="true" language="c#" Codebehind="CtlUpdatePage.aspx.cs" AutoEventWireup="false"
Inherits="ControlDemo.CtlUpdatePage" %>
<%@ Register TagPrefix="max" TagName="CtlUpdate" Src="CtlUpdate.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
</HEAD>
<body MS_POSITIONING="GridLayout">

<form id="CtlUpdatePage" method="post" runat="server">

<max:CtlUpdate id=CtlUpdate1 runat="server" sql="Select * From Contacts"></max:CtlUpdate>

</form>

</body>
</HTML>

4.In the Page_Load event of Codebehind="CtlUpdatePage.aspx.cs" assign a connection
to the connection property of the UserControl.

Now change the select statement to any valid SQL select and change the connection
to any OleDbConnection and the UserControl does all of the work to generate
a new update form.

This first version does not contain robust error handling and all generated
controls are TextBoxes. Future versions will have ComboBoxes, Check Boxes
and error handling.
Your thoughts,
Max