Stephen,
You'll remember that we displayed a collection/database of book titles in a datagrid control and edited the display narrowing it down to sub-catagories using button controls. Our code looked something like the following:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string DbPath = Server.MapPath("bin/BookTest.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM Books ORDER BY Title";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
//Set the properties of dgTestIt datagrid control
dgTestIt.AlternatingItemStyle.BackColor=System.Drawing.Color.LightBlue;
dgTestIt.ItemStyle.BackColor=System.Drawing.Color.LightGreen;
dgTestIt.AllowSorting=false;
dgTestIt.ShowFooter=true;
dgTestIt.ShowHeader=true;
dgTestIt.Width=800;
dgTestIt.BackColor=System.Drawing.Color.LightGray;
dgTestIt.BorderColor=System.Drawing.Color.Green;
dgTestIt.BorderStyle=BorderStyle.Solid;
dgTestIt.BorderWidth=3;
dgTestIt.Font.Size=8;
btnSQL.BackColor=System.Drawing.Color.LightGreen;
btnSQL.BorderColor=System.Drawing.Color.ForestGreen;
btnSQL.BorderStyle=BorderStyle.Ridge;
btnAll.BackColor=System.Drawing.Color.LightGreen;
btnAll.BorderColor=System.Drawing.Color.ForestGreen;
btnAll.BorderStyle=BorderStyle.Ridge;
btnASP.BackColor=System.Drawing.Color.LightGreen;
btnASP.BorderColor=System.Drawing.Color.ForestGreen;
btnASP.BorderStyle=BorderStyle.Ridge;
btnCSharp.BackColor=System.Drawing.Color.LightGreen;
btnCSharp.BorderColor=System.Drawing.Color.ForestGreen;
btnCSharp.BorderStyle=BorderStyle.Ridge;
btnVB.BackColor=System.Drawing.Color.LightGreen;
btnVB.BorderColor=System.Drawing.Color.ForestGreen;
btnVB.BorderStyle=BorderStyle.Ridge;
btnGameProgramming.BackColor=System.Drawing.Color.LightGreen;
btnGameProgramming.BorderColor=System.Drawing.Color.ForestGreen;
btnGameProgramming.BorderStyle=BorderStyle.Ridge;
btnDirectX.BackColor=System.Drawing.Color.LightGreen;
btnDirectX.BorderColor=System.Drawing.Color.ForestGreen;
btnDirectX.BorderStyle=BorderStyle.Ridge;
btnCplusplus.BackColor=System.Drawing.Color.LightGreen;
btnCplusplus.BorderColor=System.Drawing.Color.ForestGreen;
btnCplusplus.BorderStyle=BorderStyle.Ridge;
lblLibrary.BackColor=System.Drawing.Color.LightGreen;
lblLibrary.BorderColor=System.Drawing.Color.ForestGreen;
lblLibrary.BorderStyle=BorderStyle.Ridge;
lblLibrary.Font.Size=12;
lblLibrary.Text="Welcome to My Programming Library. Please click one of the buttons below to choose which library you would like to view.";
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSQL.Click += new System.EventHandler(this.btnSQL_Click);
this.btnAll.Click += new System.EventHandler(this.btnAll_Click);
this.btnASP.Click += new System.EventHandler(this.btnASP_Click);
this.btnCSharp.Click += new System.EventHandler(this.btnCSharp_Click);
this.btnCplusplus.Click += new System.EventHandler(this.btnCplusplus_Click);
this.btnDirectX.Click += new System.EventHandler(this.btnDirectX_Click);
this.btnGameProgramming.Click += new System.EventHandler(this.btnGameProgramming_Click);
this.btnVB.Click += new System.EventHandler(this.btnVB_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnAll_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM ASPNET UNION SELECT * FROM VB UNION SELECT * FROM Cpp UNION SELECT * FROM Game UNION SELECT * FROM CSharp UNION SELECT * FROM SeQuil UNION SELECT * FROM DX ORDER BY Title ";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
//dgTestIt.PageSize=8;
dgTestIt.DataBind();
MyOleDbConn.Close();
}
private void btnASP_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM ASPNET ORDER BY Title";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
private void btnCSharp_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM CSharp ORDER BY Title";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
private void btnCplusplus_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM Cpp ORDER BY Title";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
private void btnDirectX_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM DX ORDER BY Title";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
private void btnGameProgramming_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM Game";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
private void btnVB_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM VB ORDER BY Title";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
private void btnSQL_Click(object sender, System.EventArgs e)
{
string DbPath = Server.MapPath("bin/LibBooksProg.mdb");
//string ConnStr;
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+
@"Data Source="+DbPath+";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
string SQL= "SELECT * FROM SeQuil ORDER BY Title";
OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn);
MyOleDbConn.Open();
dgTestIt.DataSource= Commandobj.ExecuteReader();
dgTestIt.DataBind();
MyOleDbConn.Close();
}
Effective but messay as hell to maintian and a real nightmare for someone else to read and understand quickly. In other words, this code is uglier than a 90 year old whore's butt in winter.
In my next post I'll show how we cleaned up this mess and reduced the number of lines of code needed by approx. 60%.