How to create code generator for your data objects, data access layer and business access layer?

This post of mine shows how I create DAL, BAL, Data entity objects, interface. This code generator will generate code for this .cs file. First, this is how it looks like when you are creating DAL, Second, this is how it looks like when you are creating BAL, Third, this is how it looks like when you are creating data entity objects, Fourth, this is how it looks like when you are creating the interface, Here is the code,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace CSharpCodeGenerator
{
public partial class CodeGenerator : Form
{
StringBuilder sb;
string connstring = “Data Source=DEV-ARNOLD;Initial Catalog=TestDb;User ID=sa;Password=passw0rd;”;
SqlConnection conn;
SqlDataReader reader;
string dal;
string data;
string business;
string tablename;
string ConString;
string filename;
DataTable dt;
DataSet ds;
List tableList;
string foreign;
List foreignList;
string table;
string className;

public CodeGenerator()
{
InitializeComponent();
sb = new StringBuilder();
conn = new SqlConnection(connstring);
if (conn.State == ConnectionState.Closed)
conn.Open();
dal = “TestDataLayer”;
data = “TestDataEntity”;
business = “TestBusinessLayer”;
ConString = “TestSqlConnectionString”;
dt = new DataTable();
ds = new DataSet();
tableList = new List();
foreign = string.Empty;
foreignList = new List();
table = string.Empty;
className = string.Empty;
RetrieveAllTables();
PopulateGrid();
PopulateNamespaceList();
if (table != string.Empty)
tablename = table.Substring(3);
}

private void ResetEntry()
{
sb = new StringBuilder();
this.dgvField.DataSource = null;
this.txtGeneratedCode.Text = string.Empty;
}

private void PopulateGrid()
{
try
{
if (table == string.Empty)
return;
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = “select column_name AS ColumnName, data_type + case isnull(character_maximum_length,”) when ” then ” else ‘(‘ + cast (character_maximum_length AS varchar(6)) + ‘)’ end AS DataType, case is_nullable when ‘YES’ then ‘null’ else ‘not null’ end AS Nullable from information_schema.columns where table_name = ‘” + this.cboTables.Text.Trim() + “‘ order by ordinal_position”;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
reader.Close();
reader = cmd.ExecuteReader();
dt = new DataTable(“information_schema.columns”);
ds = new DataSet();
ds.Tables.Add(dt);
ds.Load(reader, LoadOption.PreserveChanges, ds.Tables[0]);
this.dgvField.DataSource = ds.Tables[0];
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}

private void RetrieveAllTables()
{
try
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = “select distinct name from sysobjects where xtype=’U'”;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
reader = cmd.ExecuteReader();
this.cboTables.Items.Clear();
string select = string.Empty;
while (reader.Read())
{
this.cboTables.Items.Add(reader[0]);
select = Convert.ToString(reader[0]).Substring(3);
if (tableList == null)
tableList = new List();
if (tableList.Count == 0)
tableList.Add(select);
else
if (!tableList.Contains(select))
tableList.Add(select);
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
finally
{
reader.Close();
}
}

private void RetrieveForeignKeyList()
{
try
{
RetrieveAllTables();
List list = new List();
foreach (DataRow r in ds.Tables[0].Rows)
{
foreign = Convert.ToString(r[0]).Replace(tablename, “”);
list.Add(foreign);
}
foreach (string f in list)
{
foreach (string s in tableList)
{
if (f.Contains(s))
{
foreign = s;
foreignList.Add(foreign);
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}

private void PopulateNamespaceList()
{
try
{
this.cboNamespace.Items.Clear();
this.cboNamespace.Items.Add(dal);
this.cboNamespace.Items.Add(data);
this.cboNamespace.Items.Add(business);
this.cboNamespace.Items.Add(“Interface”);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}

private void GenerateCode(string name)
{
try
{
string var = string.Empty;
if (table != string.Empty)
tablename = table.Substring(3);
RetrieveForeignKeyList();
ResetEntry();
PopulateGrid();
if (dal == name)
{
sb.Append(“using System;”);
sb.Append(Environment.NewLine + “using System.Collections.Generic;”);
sb.Append(Environment.NewLine + “using System.Text;”);
sb.Append(Environment.NewLine + “using System.Data;”);
sb.Append(Environment.NewLine + “using System.Data.SqlClient;”);
sb.Append(Environment.NewLine + “using System.Configuration;”);
sb.Append(Environment.NewLine + “using ” + data + “;”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “namespace ” + dal);
sb.Append(Environment.NewLine + “{“);
filename = “cls” + tablename + “DAL”;
sb.Append(Environment.NewLine + “\t public class ” + filename + “: I” + tablename);
sb.Append(Environment.NewLine + “\t {“);
sb.Append(Environment.NewLine + “\t\t SqlConnection sqlConn;”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public cls” + tablename + “DAL()”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings[” + “\”TestSqlConnectionString\”” + “].ConnectionString);”);
sb.Append(Environment.NewLine + “\t\t\t sqlConn.Open();”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public int Insert” + tablename + “(” + tablename + “Entity ” + tablename.ToLower() + “)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t int result = 0;”);
sb.Append(Environment.NewLine + “\t\t\t using (SqlCommand cmd = sqlConn.CreateCommand())”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandText = ” + “\”spInsert” + tablename + “\”;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandType = CommandType.StoredProcedure;”);
foreach (DataRow rw in ds.Tables[0].Rows)
sb.Append(Environment.NewLine + “\t\t\t\t cmd.Parameters.AddWithValue(” + “\”@” + Convert.ToString(rw[0]) + “\”, ” + tablename.ToLower() + “.” + Convert.ToString(rw[0]) + “);”);
sb.Append(Environment.NewLine + “\t\t\t\t result = cmd.ExecuteNonQuery();”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return result;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public int Update” + tablename + “(” + tablename + “Entity ” + tablename.ToLower() + “)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t int result = 0;”);
sb.Append(Environment.NewLine + “\t\t\t using (SqlCommand cmd = sqlConn.CreateCommand())”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandText = ” + “\”spUpdate” + tablename + “\”;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandType = CommandType.StoredProcedure;”);
foreach (DataRow rw in ds.Tables[0].Rows)
sb.Append(Environment.NewLine + “\t\t\t\t cmd.Parameters.AddWithValue(” + “\”@” + Convert.ToString(rw[0]) + “\”, ” + tablename.ToLower() + “.” + Convert.ToString(rw[0]) + “);”);
sb.Append(Environment.NewLine + “\t\t\t\t result = cmd.ExecuteNonQuery();”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return result;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public int Delete” + tablename + “(int ” + tablename.ToLower() + “Id)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t int result = 0;”);
sb.Append(Environment.NewLine + “\t\t\t using (SqlCommand cmd = sqlConn.CreateCommand())”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandText = ” + “\”spDelete” + tablename + “\”;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandType = CommandType.StoredProcedure;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.Parameters.AddWithValue(” + “\”@” + tablename + “Id” + “\”, ” + tablename.ToLower() + “Id);”);
sb.Append(Environment.NewLine + “\t\t\t\t result = cmd.ExecuteNonQuery();”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return result;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public ” + tablename + “Entity Retrieve” + tablename + “ById(int ” + tablename.ToLower() + “Id)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t ” + tablename + “Entity ” + tablename.ToLower() + ” = new ” + tablename + “Entity();”);
sb.Append(Environment.NewLine + “\t\t\t using (SqlCommand cmd = sqlConn.CreateCommand())”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandText = ” + “\”spGet” + tablename + “ById\”;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandType = CommandType.StoredProcedure;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.Parameters.AddWithValue(” + “\”@” + tablename + “Id\”, ” + tablename.ToLower() + “Id);”);
sb.Append(Environment.NewLine + “\t\t\t\t using (SqlDataReader dr = cmd.ExecuteReader())”);
sb.Append(Environment.NewLine + “\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t while (dr.Read())”);
sb.Append(Environment.NewLine + “\t\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t\t ” + tablename.ToLower() + ” = Load” + tablename + “Entity(dr);”);
sb.Append(Environment.NewLine + “\t\t\t\t\t\t break;”);
sb.Append(Environment.NewLine + “\t\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public ” + tablename + “Entity Retrieve” + tablename + “ByName(string ” + tablename.ToLower() + “Name)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t ” + tablename + “Entity ” + tablename.ToLower() + ” = new ” + tablename + “Entity();”);
sb.Append(Environment.NewLine + “\t\t\t using (SqlCommand cmd = sqlConn.CreateCommand())”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandText = ” + “\”spGet” + tablename + “ByName\”;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandType = CommandType.StoredProcedure;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.Parameters.AddWithValue(” + “\”@” + tablename + “Name\”, ” + tablename.ToLower() + “Name);”);
sb.Append(Environment.NewLine + “\t\t\t\t using (SqlDataReader rd = cmd.ExecuteReader())”);
sb.Append(Environment.NewLine + “\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t while (rd.Read())”);
sb.Append(Environment.NewLine + “\t\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t\t ” + tablename.ToLower() + ” = Load” + tablename + “Entity(rd);”);
sb.Append(Environment.NewLine + “\t\t\t\t\t\t break;”);
sb.Append(Environment.NewLine + “\t\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public List Retrieve” + tablename + “List()”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t List ” + tablename.ToLower() + “List = new List();”);
sb.Append(Environment.NewLine + “\t\t\t using (SqlCommand cmd = sqlConn.CreateCommand())”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandText = ” + “\”spGet” + tablename + “List\”;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandType = CommandType.StoredProcedure;”);
sb.Append(Environment.NewLine + “\t\t\t\t using (SqlDataReader rd = cmd.ExecuteReader())”);
sb.Append(Environment.NewLine + “\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t while (rd.Read())”);
sb.Append(Environment.NewLine + “\t\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t\t ” + tablename.ToLower() + “List.Add(Load” + tablename + “Entity(rd));”);
sb.Append(Environment.NewLine + “\t\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “List;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public List Search” + tablename + “List(string ” + tablename.ToLower() + “Name)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t List ” + tablename.ToLower() + “List = new List();”);
sb.Append(Environment.NewLine + “\t\t\t using (SqlCommand cmd = sqlConn.CreateCommand())”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandText = ” + “\”spSearch” + tablename + “List\”;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.CommandType = CommandType.StoredProcedure;”);
sb.Append(Environment.NewLine + “\t\t\t\t cmd.Parameters.AddWithValue(” + “\”@” + tablename + “Name\”, ” + tablename.ToLower() + “Name);”);
sb.Append(Environment.NewLine + “\t\t\t\t using (SqlDataReader rd = cmd.ExecuteReader())”);
sb.Append(Environment.NewLine + “\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t while (rd.Read())”);
sb.Append(Environment.NewLine + “\t\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t\t ” + tablename.ToLower() + “List.Add(Load” + tablename + “Entity(rd));”);
sb.Append(Environment.NewLine + “\t\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “List;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “\t }”);
sb.Append(Environment.NewLine + “}”);
sb.Append(Environment.NewLine + “”);
}
else if (data == name)
{
sb.Append(“using System;”);
sb.Append(Environment.NewLine + “using System.Collections.Generic;”);
sb.Append(Environment.NewLine + “using System.Text;”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “namespace ” + data);
sb.Append(Environment.NewLine + “{“);
filename = tablename + “Entity”;
sb.Append(Environment.NewLine + “\t public class ” + tablename + “Entity”);
sb.Append(Environment.NewLine + “\t {“);
foreach (DataRow rw in ds.Tables[0].Rows)
{
var = Convert.ToString(rw[0]).Substring(0, 1).ToLower() + Convert.ToString(rw[0]).Substring(1, Convert.ToString(rw[0]).Length – 1);
if (Convert.ToString(rw[1]) == “int”)
sb.Append(Environment.NewLine + “\t\t private ” + Convert.ToString(rw[1]) + ” _” + var + “;”);
else
sb.Append(Environment.NewLine + “\t\t private string _” + Convert.ToString(rw[0]) + “;”);
}
foreach (string forKey in foreignList)
sb.Append(Environment.NewLine + “\t\t private ” + forKey + “Entity ” + forKey.ToLower() + “;”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public ” + tablename + “Entity()”);
sb.Append(Environment.NewLine + “\t\t {“);
foreach (string forKey in foreignList)
sb.Append(Environment.NewLine + “\t\t\t ” + forKey.ToLower() + ” = new ” + forKey + “Entity();”);
sb.Append(Environment.NewLine + “\t\t }”);
foreach (DataRow row in ds.Tables[0].Rows)
{
var = Convert.ToString(row[0]).Substring(0, 1).ToLower() + Convert.ToString(row[0]).Substring(1, Convert.ToString(row[0]).Length – 1);
sb.Append(Environment.NewLine + “”);
if (Convert.ToString(row[1]) == “int”)
sb.Append(Environment.NewLine + “\t\t public int ” + Convert.ToString(row[0]));
else
sb.Append(Environment.NewLine + “\t\t public string ” + Convert.ToString(row[0]));
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t get”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t return _” + var + “;”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t set”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t if (_” + var + ” != value)”);
sb.Append(Environment.NewLine + “\t\t\t\t\t _” + var + “= value;”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t }”);
}
foreach (string f in foreignList)
{
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public string ” + f + “Name”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t get”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t return ” + f.ToLower() + “.” + f + “Name;”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t set”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t if (” + f.ToLower() + “.” + f + “Name != value)”);
sb.Append(Environment.NewLine + “\t\t\t\t\t ” + f.ToLower() + “.” + f + “Name = value;”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t }”);
}
sb.Append(Environment.NewLine + “\t }”);
sb.Append(Environment.NewLine + “}”);
sb.Append(Environment.NewLine + “”);
}
else if (business == name)
{
sb.Append(“using System;”);
sb.Append(Environment.NewLine + “using System.Collections.Generic;”);
sb.Append(Environment.NewLine + “using System.Text;”);
sb.Append(Environment.NewLine + “using ” + dal + “;”);
sb.Append(Environment.NewLine + “using ” + data + “;”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “namespace ” + business);
sb.Append(Environment.NewLine + “{“);
filename = “cls” + tablename + “BAL”;
sb.Append(Environment.NewLine + “\t public class ” + filename);
sb.Append(Environment.NewLine + “\t {“);
sb.Append(Environment.NewLine + “\t\t cls” + tablename + “DAL ” + tablename.ToLower() + “DAL;”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public cls” + tablename + “BAL()”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t cityDAL = new cls” + tablename + “DAL();”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public int Insert(” + tablename + “Entity ” + tablename.ToLower() + “)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “DAL.Insert” + tablename + “(” + tablename.ToLower() + “);”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public int Update(” + tablename + “Entity ” + tablename.ToLower() + “)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “DAL.Update” + tablename + “(” + tablename.ToLower() + “);”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public int Delete(int ” + tablename.ToLower() + “Id)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “DAL.Delete” + tablename + “(” + tablename.ToLower() + “Id);”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public List GetAll” + tablename.Substring(0, tablename.Length – 1) + “ies()”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “DAL.Retrieve” + tablename + “List();”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public ” + tablename + “Entity Get” + tablename + “ById(int ” + tablename.ToLower() + “Id)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “DAL.Retrieve” + tablename + “ById(” + tablename.ToLower() + “Id);”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public ” + tablename + “Entity Get” + tablename + “ByName(string ” + tablename.ToLower() + “Name)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “DAL.Retrieve” + tablename + “ByName(” + tablename.ToLower() + “Name);”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public List Search” + tablename + “(string ” + tablename.ToLower() + “Name)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t return ” + tablename.ToLower() + “DAL.Search” + tablename + “List(” + tablename + “Name);”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public bool CheckForNewDuplicate” + tablename + “Name(string ” + tablename.ToLower() + “)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t bool duplicate = false;”);
sb.Append(Environment.NewLine + “\t\t\t List ” + tablename.ToLower() + “List = new List();”);
sb.Append(Environment.NewLine + “\t\t\t ” + tablename.ToLower() + “List = ” + tablename.ToLower() + “DAL.Retrieve” + tablename + “List();”);
sb.Append(Environment.NewLine + “\t\t\t foreach (” + tablename + “Entity c in ” + tablename.ToLower() + “List)”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t if (c.” + tablename + “Name.ToUpper() == ” + tablename.ToLower() + “.ToUpper())”);
sb.Append(Environment.NewLine + “\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t duplicate = true;”);
sb.Append(Environment.NewLine + “\t\t\t\t\t break;”);
sb.Append(Environment.NewLine + “\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return duplicate;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “\t\t public bool CheckForExistingDuplicate” + tablename + “Name(string ” + tablename.ToLower() + “, int ” + tablename.ToLower() + “Id)”);
sb.Append(Environment.NewLine + “\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t bool duplicate = false;”);
sb.Append(Environment.NewLine + “\t\t\t List ” + tablename.ToLower() + “List = new List();”);
sb.Append(Environment.NewLine + “\t\t\t ” + tablename.ToLower() + “List = ” + tablename.ToLower() + “DAL.Retrieve” + tablename + “List();”);
sb.Append(Environment.NewLine + “\t\t\t foreach (” + tablename + “Entity c in ” + tablename.ToLower() + “List)”);
sb.Append(Environment.NewLine + “\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t if (c.” + tablename + “Name.ToUpper() == ” + tablename.ToLower() + “.ToUpper() & c.” + tablename + “Id != ” + tablename.ToLower() + “Id)”);
sb.Append(Environment.NewLine + “\t\t\t\t {“);
sb.Append(Environment.NewLine + “\t\t\t\t\t duplicate = true;”);
sb.Append(Environment.NewLine + “\t\t\t\t\t break;”);
sb.Append(Environment.NewLine + “\t\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t }”);
sb.Append(Environment.NewLine + “\t\t\t return duplicate;”);
sb.Append(Environment.NewLine + “\t\t }”);
sb.Append(Environment.NewLine + “\t }”);
sb.Append(Environment.NewLine + “}”);
sb.Append(Environment.NewLine + “”);
}
else if (name == “Interface”)
{
sb.Append(“using System;”);
sb.Append(Environment.NewLine + “using System.Collections.Generic;”);
sb.Append(Environment.NewLine + “using System.Text;”);
sb.Append(Environment.NewLine + “using TestDataEntity;”);
sb.Append(Environment.NewLine + “using System.Configuration;”);
sb.Append(Environment.NewLine + “”);
sb.Append(Environment.NewLine + “namespace ” + business);
sb.Append(Environment.NewLine + “{“);
filename = “I” + tablename;
sb.Append(Environment.NewLine + “\t public interface ” + filename);
sb.Append(Environment.NewLine + “\t {“);
sb.Append(Environment.NewLine + “\t\t int InsertCity(CityEntity city);”);
sb.Append(Environment.NewLine + “\t\t int UpdateCity(CityEntity city);”);
sb.Append(Environment.NewLine + “\t\t int DeleteCity(int cityId);”);
sb.Append(Environment.NewLine + “\t\t List RetrieveCityList();”);
sb.Append(Environment.NewLine + “\t\t CityEntity RetrieveCityById(int cityId);”);
sb.Append(Environment.NewLine + “\t\t CityEntity RetrieveCityByName(string cityName);”);
sb.Append(Environment.NewLine + “\t\t List SearchCityList(string cityName);”);
sb.Append(Environment.NewLine + “\t }”);
sb.Append(Environment.NewLine + “}”);
sb.Append(Environment.NewLine + “”);
}
this.txtGeneratedCode.Text = sb.ToString();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}

private void btnGenerate_Click(object sender, EventArgs e)
{
try
{
if (className != string.Empty)
GenerateCode(className);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}

private void btnReset_Click(object sender, EventArgs e)
{
ResetEntry();
}

private void cboTables_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (this.cboTables.SelectedItem != null)
table = this.cboTables.SelectedItem.ToString();
PopulateGrid();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}

private void btnGenerateFile_Click(object sender, EventArgs e)
{
try
{
if (className != string.Empty)
GenerateCode(className);
string path = @”C:\GeneratedCode\” + filename + “.cs”;
if (!File.Exists(path))
File.WriteAllText(path, sb.ToString());
else
MessageBox.Show(“File already exists !!!”);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}

private void cboNamespace_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (this.cboNamespace.SelectedItem != null)
className = this.cboNamespace.SelectedItem.ToString();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
}
}

Advertisements
This entry was posted in ASP.NET 3.5, ASP.NET 3.5 And C# 3.5, C# 3.5, C# 3.5 and ADO.NET Entity Framework 3.5. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s