473,756 Members | 4,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stuck with Idatareader object in N-Tier Architecture

149 New Member
Hi,

I am new to ASP.NET and I am trying to build a CMS application using theBeerHouse SK.

I am adapting the Datareader to collect information from an SQL database and send it to an HtmlHeader. Using N-Tier architecture I keep recieving an object reference required error.

Here's my code:

Home.aspx.cs

Expand|Select|Wrap|Line Numbers
  1. namespace TMG.DAL
  2. {
  3.     public partial class Home : System.Web.UI.Page
  4.     {
  5.         public HeaderDetails findInfo(string pageId)
  6.         {
  7.             HeaderDetails headers = null;
  8.             headers = GetHeader.GetHeaderById(pageId);
  9.             return headers;
  10.         }
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {
  13.             String pageId = Page.GetType().Name;
  14.             pageId = pageId.Replace("_aspx", "");
  15.             Master.Page.Header.Title = "TMG - Advertisers site - " + pageId;
  16.             //Get the htmlHead your aspx page is using (from the Master page)
  17.             //Master page must include the runat server attribute in the head tag: <head runat="server">
  18.             HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)Header;
  19.  
  20.             //Create a htmlMeta object
  21.  
  22.             HtmlMeta meta = new HtmlMeta();
  23.             HtmlMeta meta2 = new HtmlMeta();
  24.             HtmlMeta meta3 = new HtmlMeta();
  25.  
  26.             //Specify meta attributes
  27.             meta.Attributes.Add("name", "keywords");
  28.             meta.Attributes.Add("content", "blah");
  29.             meta2.Attributes.Add("name", "description");
  30.             meta2.Attributes.Add("content", "blah");
  31.             meta3.Attributes.Add("name", "abstract");
  32.             meta3.Attributes.Add("content", "blah");
  33.             // Add the meta object to the htmlhead's control collection
  34.             head.Controls.Add(meta);
  35.             head.Controls.Add(meta2);
  36.             head.Controls.Add(meta3);
  37.         }
  38.     }
  39. }
  40.  
  41. HeaderProvider.cs
  42.  
  43. namespace TMG.DAL
  44. {
  45.     public abstract class HeaderProvider : DataAccess
  46.     {
  47.         //public abstract HeaderDetails GetHeaderByID(string pageId);
  48.  
  49.         protected virtual HeaderDetails GetHeaderFromReader(IDataReader reader)
  50.         {
  51.             return GetHeaderFromReader(reader, true);
  52.         }
  53.         protected virtual HeaderDetails GetHeaderFromReader(IDataReader reader, bool readBody)
  54.         {
  55.             HeaderDetails headers = new HeaderDetails(
  56.                 (int)reader["id"],
  57.                null,
  58.                null,
  59.                null,
  60.                reader["javascript"].ToString(),
  61.                reader["title"].ToString(),
  62.                reader["css"].ToString(),
  63.                reader["page_id"].ToString());
  64.  
  65.             if (readBody)
  66.                 headers.AbstractText = reader["abstractText"].ToString();
  67.                 headers.Description = reader["description"].ToString();
  68.                 headers.Keywords = reader["keywords"].ToString();
  69.  
  70.             return headers;
  71.         }
  72.     }
  73. }
  74.  
  75. HeaderDetails.cs
  76.  
  77. namespace TMG.DAL
  78. {
  79.     public class HeaderDetails
  80.     {
  81.         public HeaderDetails(int id, string abstractText, string description, string keywords, string javascript, string title, string css, string page_id)
  82.         {
  83.             this.ID = id;
  84.             this.AbstractText = abstractText;
  85.             this.Description = description;
  86.             this.Keywords = keywords;
  87.             this.Javascript = javascript;
  88.             this.Title = title;
  89.             this.Css = css;
  90.             this.PageId = page_id;
  91.  
  92.  
  93.         }
  94.         private int _id = 0;
  95.         public int ID
  96.         {
  97.             get { return _id; }
  98.             set { _id = value; }
  99.         }
  100.         private string _abstractText = "";
  101.         public string AbstractText
  102.         {
  103.             get { return _abstractText; }
  104.             set { _abstractText = value; }
  105.         }
  106.         private string _description = "";
  107.         public string Description
  108.         {
  109.             get { return _description; }
  110.             set { _description = value; }
  111.         }
  112.         private string _keywords = "";
  113.         public string Keywords
  114.         {
  115.             get { return _keywords; }
  116.             set { _keywords = value; }
  117.         }
  118.         private string _javascript = "";
  119.         public string Javascript
  120.         {
  121.             get { return _javascript; }
  122.             set { _javascript = value; }
  123.         }
  124.         private string _title = "";
  125.         public string Title
  126.         {
  127.             get { return _title; }
  128.             set { _title = value; }
  129.         }
  130.         private string _css = "";
  131.         public string Css
  132.         {
  133.             get { return _css; }
  134.             set { _css = value; }
  135.         }
  136.         private string _pageId = "";
  137.         public string PageId
  138.         {
  139.             get { return _pageId; }
  140.             set { _pageId = value; }
  141.         }
  142.     }
  143. }
  144.  
  145. GetHeader.cs
  146. namespace TMG.DAL
  147. {
  148.     public class GetHeader : HeaderProvider
  149.     {
  150.         private int _id = 0;
  151.         public int ID
  152.         {
  153.             get { return _id; }
  154.             set { _id = value; }
  155.         }
  156.         private string _abstractText = "";
  157.         public string AbstractText
  158.         {
  159.             get { return _abstractText; }
  160.             set { _abstractText = value; }
  161.         }
  162.         private string _description = "";
  163.         public string Description
  164.         {
  165.             get { return _description; }
  166.             set { _description = value; }
  167.         }
  168.         private string _keywords = "";
  169.         public string Keywords
  170.         {
  171.             get { return _keywords; }
  172.             set { _keywords = value; }
  173.         }
  174.         private string _javascript = "";
  175.         public string Javascript
  176.         {
  177.             get { return _javascript; }
  178.             set { _javascript = value; }
  179.         }
  180.         private string _title = "";
  181.         public string Title
  182.         {
  183.             get { return _title; }
  184.             set { _title = value; }
  185.         }
  186.         private string _css = "";
  187.         public string Css
  188.         {
  189.             get { return _css; }
  190.             set { _css = value; }
  191.         }
  192.         private string _pageId = "";
  193.         public string PageId
  194.         {
  195.             get { return _pageId; }
  196.             set { _pageId = value; }
  197.         }
  198.         public GetHeader(int id, string abstractText, string description, string keywords, string javascript, string title, string css, string page_id)
  199.         {
  200.             this.ID = id;
  201.             this.AbstractText = abstractText;
  202.             this.Description = description;
  203.             this.Keywords = keywords;
  204.             this.Javascript = javascript;
  205.             this.Title = title;
  206.             this.Css = css;
  207.             this.PageId = page_id;
  208.  
  209.  
  210.         }
  211.  
  212.         public virtual HeaderDetails GetHeaderById(string pageId)
  213.         {
  214.            SqlConnection cn = GetConnection();
  215.            SqlCommand oCommand = new SqlCommand("page_header",cn);
  216.            oCommand.CommandType=CommandType.StoredProcedure; // Parametrs
  217.            oCommand.Parameters.Add("@pageId", SqlDbType.VarChar).Value = pageId;
  218.            cn.Open();
  219.            IDataReader reader = ExecuteReader(oCommand, CommandBehavior.SingleRow);
  220.            if (reader.Read())
  221.                return GetHeaderFromReader(reader, true);
  222.            else
  223.                return null;
  224.         }
  225.     }
  226. }
  227.  
  228. DataAccess.cs
  229. namespace TMG.DAL
  230. {
  231.     public abstract class DataAccess
  232.     {
  233.         protected static string strConnect;
  234.         public DataAccess()
  235.         {
  236.         } 
  237.  
  238.         static DataAccess()
  239.         {
  240.             strConnect = ConfigurationManager.AppSettings["connstr"];
  241.         } 
  242.  
  243.         protected SqlConnection GetConnection()
  244.         {   
  245.             SqlConnection oConnection = new SqlConnection(strConnect);
  246.             return oConnection;
  247.         }
  248.         protected IDataReader ExecuteReader(DbCommand cmd)
  249.         {
  250.             return ExecuteReader(cmd, CommandBehavior.Default);
  251.         }
  252.  
  253.         protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior)
  254.         {
  255.             return cmd.ExecuteReader(behavior);
  256.         }
  257.         protected object ExecuteScalar(DbCommand cmd)
  258.         {
  259.             return cmd.ExecuteScalar();
  260.         }
  261.     }
  262. }
Sorry for all the code.
Jul 10 '07 #1
0 1314

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1979
by: Tamir Khason | last post by:
As far as I understand, IDataReader, based on command recieves isClosed property when the connection closes. How to prevent it?
2
11750
by: Vivek Sharma | last post by:
Hi There, can some one please clarify the difference between SQLDataReader and IDataReader? What are the advantages and disadvantages of each? Thanks Vivek
5
7037
by: Gelios | last post by:
Hello All! I am going to crazy and feeling myself so stupid but I don't understand such behaviour. I have code: public int getNextAgentId() { Int32 agent_id = 0; IDataReader dr = dbap.DBDataReader("SELECT MAX(agent_id) FROM Agents"); if(dr.Read()) { agent_id = dr.GetInt32(0);
4
4601
by: Mahesh Kumar.R | last post by:
What is the difference between SqlDataReader and IDataReader ...? kindly with small example... Mahesh~
3
2367
by: js | last post by:
Hi, I want to debug the return idatareader, how to print out some fields value? many thanks.
2
1566
by: יוני גולדברג | last post by:
Hi, In few places within my code the business object pass IDataReader to the GUI. Suddenly i noticed that nowhere in the code the IDataReader is being closed. Does the data binding operation closes the IDataReader? If i didn't close it explicitly, the IDataReader remains open untill GC will clean it?
2
2088
by: Larry R | last post by:
Whenever I try the following, the reader that is returned is always closed. What am I missing ? When I look at the reader in the ExecuteReader, it is fine. THen it gets closed on the returm. Thanks! ============================
2
2865
by: RP | last post by:
I saw a code in a class where IDataReader is used to retrieve result from query, even if only one column is to be retrieved. It uses a CommandObject with ExecuteReader but not using ExecuteScalar. For what IDataReader is used and what are the benefits of it as compared to sime DataReader.
1
1239
parshupooja
by: parshupooja | last post by:
Hey All, I have 2 IDatareaders. forex: Idatareader r One Two Three Four Idatareader a One
0
1174
by: Andrus | last post by:
RDLDesigner report designer requires IDataReader for data access. To use it with Linq, I need to convert Linq projection to IDataReader. var db = new NorthwindContext(); var q = from c in db.Customers select new { c.ContactName, c.City }; IDataReader dr = QueryDataReader( db, q );
0
9844
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
9819
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
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
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
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
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
3
2647
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.