Home
Manage Your Code
Snippet: Form Generator (CodeSmith)
Title: Form Generator Language: CodeSmith
Description: This generates a form from a table Views: 249
Author: Kevin Isom Date Added: 11/25/2007
Copy Code  
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a form based on a table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>

<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<% 

ColumnSchema thisColumn;
string colName="";
string colType="";
bool IsNullable=false;
%>
	
<div id="divAdd" class="inputForm">
<%
	for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { 
		thisColumn=SourceTable.NonPrimaryKeyColumns[i];
		colName=GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]).Replace(" ","");
		colType=GetCSharpVariableType(thisColumn);
		IsNullable=thisColumn.AllowDBNull;
		%>
		<div class="formRow">
		<asp:label AssociatedControlID="_<%=colName%>" runat="server"><%=colName%></asp:label>
		<br />
		<%
		if(colType=="DateTime"){
		%>
			<SubSonic:CalendarControl id="_<%=colName%>" runat="server">
			</SubSonic:CalendarControl>
	
		<%}else if(colType=="string"){%>
	
		<asp:textbox id="_<%=colName%>" runat="server"
				<%if(UseMultiLine(thisColumn)){%>
				TextMode="MultiLine" Height="100px" Width="400px"
				<%}%>
				></asp:textbox>
				<%if(!IsNullable){%>
				<span class="required">*</span>
				<asp:RequiredFieldValidator id="valTxt<%=colName%>" runat="server" Display="Dynamic" 
				ControlToValidate="_<%=colName%>"
					ErrorMessage="Required"></asp:RequiredFieldValidator>
				<%}%>
	
		<%}else if(IsNumberField(thisColumn) && colName.ToLower().IndexOf("id")>0){%>
		
			<SubSonic:DropDown id="_<%=colName%>" runat="server" TableName="<%= colName.Replace("Id","")%>">
			</SubSonic:DropDown>
	
		<%}else if(IsNumberField(thisColumn)){%>
				<asp:textbox id="_<%=colName%>" runat="server"
				></asp:textbox>
			<%if(!IsNullable){%>
			<span class="required">*</span>
			<asp:RequiredFieldValidator id="valTxt<%=colName%>" runat="server" Display="Dynamic" 
			ControlToValidate="_<%=colName%>"
				ErrorMessage="Required"></asp:RequiredFieldValidator>
			<%}%>
			<asp:RangeValidator id="valNum<%=colName%>" runat="server" Display="Dynamic" ControlToValidate="_<%=colName%>"
			ErrorMessage="Invalid Number" MaximumValue="99999999" MinimumValue="0" Type="Integer"></asp:RangeValidator>
		<%}else if(colType=="bool"){%>
			<asp:CheckBox id=_<%=colName%> runat=server />
			
		<%}%>
		</div>
	<% } %>

	<div>
		<asp:Button ID="btnAdd" Runat="server" Text="Add"></asp:Button>
	</div>
</div>

		
	

		
		
		
		
		
		
		
<script runat="template">
public bool UseMultiLine(ColumnSchema column){
	bool bOut=false;
	if(column.Size>200)
		bOut=true;
	return bOut;

}
public bool IsNumberField(ColumnSchema column){
	bool bOut=false;
	string vType=GetCSharpVariableType(column);
	if(vType=="decimal" || vType=="double" || vType=="short" || vType=="int" || vType=="long" || vType=="float" || vType=="ushort" || vType=="uint" || vType=="ulong"){
		bOut=true;
	}
	return bOut;
}
public string GetProperName(string sIn)
{
	string propertyName =sIn;
	string leftOne=propertyName.Substring(0,1).ToUpper();
	propertyName=propertyName.Substring(1,propertyName.Length-1);
	propertyName=leftOne+propertyName;
		
	return propertyName;
}

public string GetPropertyName(ColumnSchema column)
{
	string propertyName = column.Name;
	string leftOne=propertyName.Substring(0,1).ToUpper();
	propertyName=propertyName.Substring(1,propertyName.Length-1);
	propertyName=leftOne+propertyName;
	
	propertyName=propertyName.Replace(" ","");
	
	return propertyName;
}
		
public string GetCSharpVariableType(ColumnSchema column)
{
	if (column.Name.EndsWith("TypeCode")) return column.Name;
	
	switch (column.DataType)
	{
		case DbType.AnsiString: return "string";
		case DbType.AnsiStringFixedLength: return "string";
		case DbType.Binary: return "byte[]";
		case DbType.Boolean: return "bool";
		case DbType.Byte: return "byte";
		case DbType.Currency: return "decimal";
		case DbType.Date: return "DateTime";
		case DbType.DateTime: return "DateTime";
		case DbType.Decimal: return "decimal";
		case DbType.Double: return "double";
		case DbType.Guid: return "Guid";
		case DbType.Int16: return "short";
		case DbType.Int32: return "int";
		case DbType.Int64: return "long";
		case DbType.Object: return "object";
		case DbType.SByte: return "sbyte";
		case DbType.Single: return "float";
		case DbType.String: return "string";
		case DbType.StringFixedLength: return "string";
		case DbType.Time: return "TimeSpan";
		case DbType.UInt16: return "ushort";
		case DbType.UInt32: return "uint";
		case DbType.UInt64: return "ulong";
		case DbType.VarNumeric: return "decimal";
		default:
		{
			return "__UNKNOWN__" + column.NativeType;
		}
	}
}


public string GetSqlDbType(ColumnSchema column)
{
	switch (column.NativeType)
	{
		case "bigint": return "BigInt";
		case "binary": return "Binary";
		case "bit": return "Bit";
		case "char": return "Char";
		case "datetime": return "DateTime";
		case "decimal": return "Decimal";
		case "float": return "Float";
		case "image": return "Image";
		case "int": return "Int";
		case "money": return "Money";
		case "nchar": return "NChar";
		case "ntext": return "NText";
		case "numeric": return "Decimal";
		case "nvarchar": return "NVarChar";
		case "real": return "Real";
		case "smalldatetime": return "SmallDateTime";
		case "smallint": return "SmallInt";
		case "smallmoney": return "SmallMoney";
		case "sql_variant": return "Variant";
		case "sysname": return "NChar";
		case "text": return "Text";
		case "timestamp": return "Timestamp";
		case "tinyint": return "TinyInt";
		case "uniqueidentifier": return "UniqueIdentifier";
		case "varbinary": return "VarBinary";
		case "varchar": return "VarChar";
		default: return "__UNKNOWN__" + column.NativeType;
	}
}

public string GetPrimaryKeyType(TableSchema table)
{
	if (table.PrimaryKey != null)
	{
		if (table.PrimaryKey.MemberColumns.Count == 1)
		{
			return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
		}
		else
		{
			throw new ApplicationException("This template will not work on primary keys with more than one member column.");
		}
	}
	else
	{
		throw new ApplicationException("This template will only work on tables with a primary key.");
	}
}

</script>