|
xxx
Your first C# Library (less reflection)
by Charles Carroll and Tony
Here is the library altered to use an interface and 1 reflection call instead of 2.
filename=/experiments/Training/cs/searcher_better_v2.aspx
<%@ trace="true"%>
<%@ Assembly src="searcher_better_v2_lib.cs" %>
<script language="C#" runat="server">
string strConn;
protected void Page_Load(Object S, EventArgs E)
{
strConn="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=";
strConn += Server.MapPath(@"/experiments/data/biblio.mdb") + ";" ;
searchagain.Visible=false;
if (!Page.IsPostBack)
{
string strSQL;
strSQL="select distinct city from publishers";
lib_db.Helper.FillWebControl(strConn,strSQL,cy);
strSQL="select distinct state from publishers";
lib_db.Helper.FillWebControl(strConn,strSQL,st);
strSQL="select distinct zip from publishers";
lib_db.Helper.FillWebControl(strConn,strSQL,zp);
}
}
protected void trace_search()
{
Trace.Write("city",cy.SelectedItem.Text);
Trace.Write("state",st.SelectedItem.Text);
Trace.Write("zip",zp.SelectedItem.Text);
// Trace.Write("cityinc",cyinclude.Checked.ToString());
// Trace.Write("stateinc",stinclude.Checked.ToString());
// Trace.Write("zipinc",zpinclude.Checked.ToString());
}
protected void search_show()
{
searchcriteria.Visible=false;
search.Visible=false;
searchagain.Visible=true;
searchresults.Visible=true;
}
string search_conditions(string strFieldName,CheckBox chk1,
DropDownList drop1,ref string strParm)
{
string strTempReturn;
if (chk1.Checked)
{
strTempReturn=" " + strFieldName + "='" + drop1.SelectedItem.Text + "' ";
strParm=" AND ";
return(strTempReturn);
}
return("");
}
protected void search_click(object S, EventArgs E)
{
trace_search();
search_show();
string strSQL;
string strPrefix="";
strSQL="select * from publishers where ";
if (!(cyinclude.Checked || stinclude.Checked || zpinclude.Checked))
{
message.Text="You did not choose any categories -- we can't Search";
searchresults.Visible=false;
return;
}
else
{
message.Text="";
}
strSQL += search_conditions("city",cyinclude,cy,ref strPrefix);
strSQL += search_conditions("state",stinclude,st,ref strPrefix);
strSQL += search_conditions("zip",zpinclude,zp,ref strPrefix);
Trace.Write("strSQL",strSQL);
lib_db.Helper.FillWebControl(strConn,strSQL,searchresults);
}
public void searchagain_click(object S, EventArgs E)
{
searchcriteria.Visible=true;
searchresults.Visible=false;
searchagain.Visible=false;
search.Visible=true;
}
</script>
<html><head>
<title>Searcher</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
<asp:literal id="message" runat="server"/>
<asp:Table id="searchcriteria" GridLines="both" BorderWidth="1px" runat="server">
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell>
<ASP:DropDownList id="cy" datatextfield="city" runat="server"/>
</asp:TableCell>
<asp:TableCell>
<ASP:checkbox id="cyinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell>
<ASP:DropDownList id="st" datatextfield="state" runat="server"/>
</asp:TableCell>
<asp:TableCell>
<ASP:checkbox id="stinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Zip</asp:TableCell>
<asp:TableCell>
<ASP:DropDownList id="zp" datatextfield="zip" runat="server"/>
</asp:TableCell>
<asp:TableCell>
<ASP:checkbox id="zpinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:button id="search" text="search for data"
onclick="search_click" runat="server"/><br>
<asp:button id="searchagain" text="search for data again"
onclick="searchagain_click" runat="server"/>
<ASP:DataGrid id="searchresults" runat="server"
Width="100%"
BackColor="white"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
Headerstyle-BackColor="lightblue"
Headerstyle-Font-Size="10pt"
Headerstyle-Font-Style="bold"
MaintainState="false"
/>
</form>
</body></html>
Here is the library:
filename=/experiments/Training/cs/searcher_better_v2_lib.cs
using System;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
[assembly:AssemblyVersionAttribute("0.9.1.*")]
namespace lib_db
{
public class Helper
{
/// <summary>
/// System.Web.UI.WebControls can be filled from a OLEDB connection string and a sql statement
/// </summary>
/// <param name="pConn">Connection string</param>
/// <param name="pSQL">SQL string</param>
/// <param name="pObj">Web Control to Fill</param>
public static void FillWebControl(string pConn,string pSQL,System.Web.UI.Control pObj)
{
OleDbConnection Conn=null;
OleDbDataReader Rdr=null;
try
{
Conn = new OleDbConnection(pConn);
OleDbCommand Cmd = new OleDbCommand(pSQL, Conn);
Conn.Open();
Rdr = Cmd.ExecuteReader();
//l.DataSource = Rdr;
Type t = pObj.GetType();
PropertyInfo pi = t.GetProperty("DataSource");
if (pi!=null)
{
pi.SetValue(pObj,Rdr,null);
pObj.DataBind();
}
} // end try
catch (Exception exc1)
{
Page pageCurrent=new Page();
pageCurrent=(Page) System.Web.HttpContext.Current.Handler;
pageCurrent.Controls.Add(
new LiteralControl("Error: " + exc1.ToString() + "<br>"));
} // end catch
finally
{
if (Rdr != null)
{
if (!Rdr.IsClosed) Rdr.Close();
}
if (Conn != null)
{
if (Conn.State==System.Data.ConnectionState.Open) Conn.Close();
}
} // end finally
} // end FillWebControl
} // end class
} // end namespace
 |  |  |
 |
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too.
a $5 tip buys him lunch at McDonalds,
a $20 tip buys his kid Hitoshi a new computer game,
a $39 tip buys his daughter Michiko a few nice outfits.
See our donor list.
|  |
 |  |  |
|
|
|
|