473,387 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stuck with Idatareader object in N-Tier Architecture

149 100+
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 1298

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

Similar topics

5
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
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
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 =...
4
by: Mahesh Kumar.R | last post by:
What is the difference between SqlDataReader and IDataReader ...? kindly with small example... Mahesh~
3
by: js | last post by:
Hi, I want to debug the return idatareader, how to print out some fields value? many thanks.
2
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...
2
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. ...
2
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....
1
parshupooja
by: parshupooja | last post by:
Hey All, I have 2 IDatareaders. forex: Idatareader r One Two Three Four Idatareader a One
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.