473,662 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET 2.0 and App_Code and n-Tier

Hello --

I'm trying to rewrite a few simple ASP pages that I have to use ASP.NET 2.0.

One of the nice things I see that I can do now is separate my Business Logic
layer and Data Access Layer to use the App_Code directory.

I'm having an awful time finding any good examples of this. I've read the
examples on http://www.asp.net and various Microsoft webcasts. Most seem
very simple and I'm just wondering if I'm on the right track. I'm new to
ASP.NET 2.0.

My requirement is that I basically have a business website that has a search
form. Users basically pick a few options on the search form and the ASP
page will query a particular database and various tables and return the
results. Users can basically check the status of various jobs that are run
on the server.

I'm trying to rewrite this application the correct way and use Data Binding
along with the DataObjectSourc e to access the App_Code classes.

I've essentially duplicated the following code to query a particular table
that I have:
Business Access Layer:

using System;
public class Author
{
private String _id;

public String ID
{
get
{
return _id;
}

set
{
_id = value;
}
}

private String _name;

public String Name
{
get
{
return _name;
}
set
{ _name = value; } } private String
_lastName;

public String LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}

private String _state;

public String State
{
get
{
return _state;
}
set
{
_state = value;
}
}

public Author (String id, String name, String lastName, String state)
{
this.ID = id;
this.Name = name;
this.LastName = lastName;
this.State = state;
}

public Author()
{
// default constructor
}
}

using System;
using System.Data;
using System.Collecti ons.Generic;

public class AuthorsComponen t
{
public AuthorsComponen t ()
{
// TODO: Add constructor logic here
}

public List<Author> GetAuthorsBySta te (String state, String
sortExpression)
{
List<Author> authors = new List<Author> ();
DataSet ds = AuthorsDB.GetAu thorsByState (state);

foreach (DataRow row in ds.Tables[0].Rows)
{
authors.Add (new Author ((String)row["au_id"],
(String)row["au_fname"], (String)row["au_lname"], (String)row["state"]));
}

authors.Sort(ne w AuthorComparer( sortExpression) );
return authors;
}

public int UpdateAuthor (string ID, string LastName, string Name, string
State)
{
return AuthorsDB.Updat eAuthor (ID, LastName, Name, State);
}

public int UpdateAuthor(Au thor a)
{
return AuthorsDB.Updat eAuthor(a.ID, a.LastName, a.Name, a.State);
}

public List<String> GetStates()
{
List<String> states = new List<String>();
DataSet ds = AuthorsDB.GetSt ates();

foreach (DataRow row in ds.Tables[0].Rows)
{
states.Add((Str ing)row["state"]);
}
return states;
}
}

public class AuthorComparer : IComparer<Autho r>
{
private string _sortColumn;
private bool _reverse;

public AuthorComparer( string sortExpression)
{
_reverse = sortExpression. ToLowerInvarian t().EndsWith(" desc");
if (_reverse)
{
_sortColumn = sortExpression. Substring(0,
sortExpression. Length - 5);
}
else
{
_sortColumn = sortExpression;
}
}

public int Compare(Author a, Author b)
{
int retVal = 0;
switch (_sortColumn)
{
case "ID":
retVal = String.Compare( a.ID, b.ID,
StringCompariso n.InvariantCult ureIgnoreCase);
break;
case "Name":
retVal = String.Compare( a.Name, b.Name,
StringCompariso n.InvariantCult ureIgnoreCase);
break;
case "LastName":
retVal = String.Compare( a.LastName, b.LastName,
StringCompariso n.InvariantCult ureIgnoreCase);
break;
case "State":
retVal = String.Compare( a.State, b.State,
StringCompariso n.InvariantCult ureIgnoreCase);
break;
}
return (retVal * (_reverse ? -1 : 1));
}
}

Data Access Layer:

using System;
using System.Configur ation;

public class AuthorsDB
{
public AuthorsDB() { }

public static System.Data.Dat aSet GetAuthorsBySta te(string state)
{
string connectionStrin g =
ConfigurationMa nager.Connectio nStrings["Pubs"].ConnectionStri ng;
System.Data.IDb Connection dbConnection = new
System.Data.Sql Client.SqlConne ction(connectio nString);
string queryString = "SELECT au_id, au_fname, au_lname, state FROM
[authors] WHERE ([authors].[state] = @state)";
System.Data.IDb Command dbCommand = new
System.Data.Sql Client.SqlComma nd();

dbCommand.Comma ndText = queryString;
dbCommand.Conne ction = dbConnection;

System.Data.IDa taParameter dbParam_state = new
System.Data.Sql Client.SqlParam eter();

dbParam_state.P arameterName = "@state";
dbParam_state.V alue = state;
dbParam_state.D bType = System.Data.DbT ype.StringFixed Length;
dbCommand.Param eters.Add(dbPar am_state);

System.Data.IDb DataAdapter dataAdapter = new
System.Data.Sql Client.SqlDataA dapter();

dataAdapter.Sel ectCommand = dbCommand;

System.Data.Dat aSet dataSet = new System.Data.Dat aSet();

dataAdapter.Fil l(dataSet);
return dataSet;
}

public static System.Data.Dat aSet GetStates()
{
string connectionStrin g =
ConfigurationMa nager.Connectio nStrings["Pubs"].ConnectionStri ng;
System.Data.IDb Connection dbConnection = new
System.Data.Sql Client.SqlConne ction(connectio nString);
string queryString = "SELECT DISTINCT [authors].state FROM
[authors]";
System.Data.IDb Command dbCommand = new
System.Data.Sql Client.SqlComma nd();

dbCommand.Comma ndText = queryString;
dbCommand.Conne ction = dbConnection;

System.Data.IDb DataAdapter dataAdapter = new
System.Data.Sql Client.SqlDataA dapter();

dataAdapter.Sel ectCommand = dbCommand;

System.Data.Dat aSet dataSet = new System.Data.Dat aSet();

dataAdapter.Fil l(dataSet);
return dataSet;
}

public static int UpdateAuthor (string au_id, string au_lname, string
au_fname, string state)
{
string connectionStrin g =
ConfigurationMa nager.Connectio nStrings["Pubs"].ConnectionStri ng;
System.Data.IDb Connection dbConnection = new
System.Data.Sql Client.SqlConne ction(connectio nString);
string queryString = "UPDATE [authors] SET [au_lname]=@au_lname,
[au_fname]=@au_fname, [state]=@state WHERE ([authors].[au_id] = @au_id)";
System.Data.IDb Command dbCommand = new
System.Data.Sql Client.SqlComma nd();

dbCommand.Comma ndText = queryString;
dbCommand.Conne ction = dbConnection;

System.Data.IDa taParameter dbParam_au_id = new
System.Data.Sql Client.SqlParam eter();

dbParam_au_id.P arameterName = "@au_id";
dbParam_au_id.V alue = au_id;
dbParam_au_id.D bType = System.Data.DbT ype.String;
dbCommand.Param eters.Add(dbPar am_au_id);

System.Data.IDa taParameter dbParam_au_lnam e = new
System.Data.Sql Client.SqlParam eter();

dbParam_au_lnam e.ParameterName = "@au_lname" ;
dbParam_au_lnam e.Value = au_lname;
dbParam_au_lnam e.DbType = System.Data.DbT ype.String;
dbCommand.Param eters.Add(dbPar am_au_lname);

System.Data.IDa taParameter dbParam_au_fnam e = new
System.Data.Sql Client.SqlParam eter();

dbParam_au_fnam e.ParameterName = "@au_fname" ;
dbParam_au_fnam e.Value = au_fname;
dbParam_au_fnam e.DbType = System.Data.DbT ype.String;
dbCommand.Param eters.Add(dbPar am_au_fname);

System.Data.IDa taParameter dbParam_state = new
System.Data.Sql Client.SqlParam eter();

dbParam_state.P arameterName = "@state";
dbParam_state.V alue = state;
dbParam_state.D bType = System.Data.DbT ype.StringFixed Length;
dbCommand.Param eters.Add(dbPar am_state);

int rowsAffected = 0;

dbConnection.Op en();
try
{
rowsAffected = dbCommand.Execu teNonQuery();
}
finally
{
dbConnection.Cl ose();
}
return rowsAffected;
}
}

I really like the design of this and it seems fairly straight-forward to
follow. My question is, is this a normal scenario for access my data? I
mean, if I was query many tables and columns, would I basically have to
create a class entity for each of the queries that I'm running, similar to
what they did here for "Author" so items can be added to the "Author"
generic collection?

Also, why do they use type "string" when accessing integer fields within the
database. For example, they pass auth_id to the function as a string
instead of integer. What is the reasoning for this?

I would be interested in seeing more code similar to the above for me to use
as an example of anyone knows where I can obtain or any online resources or
books. There appears to not be much available.

I've also downloaded most of the ASP.NET 2.0 web site templates to look at
what they have.

Any help would be appreciated.

..

Feb 7 '06 #1
0 1956

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1888
by: AZ | last post by:
During the Pre-compile process of an ASP.Net 2.0 app, it compiles the code-behind & optionally the presentation files into an assembly named App_Code.dll. Can that not be renamed to a more project friendly name, such as ProjectName.dll or something. I didnt see any parameters to be able to choose the output assembly name.
11
29839
by: Steve Franks | last post by:
I'm using VS.NET 2005 Beta 2. I have a helper C# class I wrote that I placed in my /App_Code directory. Everything runs fine locally. However when I use the "Copy Web" function to upload the site to the production server, I get the following error when trying to run the page on production: "System.Web.HttpException: The directory '/App_Code' is not allowed because the application is precompiled." Anyone know this works fine locally but...
2
14578
by: pradeep_TP | last post by:
Hello, I am trying to use APP_CODE folder for all my class files under VS 2005. After adding APP_CODE in the solution explorer, I added a new web page by right clicking project and selecting add new item. I expected the code behind file (Default.aspx.cs) to automatically go under APP_CODE folder, but it didnt. I tried to drag and drop the code behind file into the APP_CODE folder. I also changed the page directive to the following: ...
9
2844
by: rn5a | last post by:
Is putting a VB class file in the special directory named App_Code the same as relocating the VB class file from the App_Code directory to another directory & then using the VBC tool, compiling the VB class file into a DLL & putting the DLL in the bin directory? Though while running an ASP.NET app using either of the 2 approaches doesn't make any difference, Visual Web Developer 2005 Express Edition behaves erratically sometimes if the...
5
3006
by: Randy | last post by:
I've converted a VS 2003 project to VS 2005. I have one utility class that it put in the APP_CODE directory. When I try and compile I'm getting this error... Error 1 The type or namespace name 'WelcomeToPFP' could not be found (are you missing a using directive or an assembly reference?) C:\Inetpub\wwwroot\PFPApp\App_Code\Utils.cs 19 9 WelcomeToPFP is a class defined in the root directory of the solution. My question is...I'm drawing a...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.