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
- namespace TMG.DAL
- {
- public partial class Home : System.Web.UI.Page
- {
- public HeaderDetails findInfo(string pageId)
- {
- HeaderDetails headers = null;
- headers = GetHeader.GetHeaderById(pageId);
- return headers;
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- String pageId = Page.GetType().Name;
- pageId = pageId.Replace("_aspx", "");
- Master.Page.Header.Title = "TMG - Advertisers site - " + pageId;
- //Get the htmlHead your aspx page is using (from the Master page)
- //Master page must include the runat server attribute in the head tag: <head runat="server">
- HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)Header;
- //Create a htmlMeta object
- HtmlMeta meta = new HtmlMeta();
- HtmlMeta meta2 = new HtmlMeta();
- HtmlMeta meta3 = new HtmlMeta();
- //Specify meta attributes
- meta.Attributes.Add("name", "keywords");
- meta.Attributes.Add("content", "blah");
- meta2.Attributes.Add("name", "description");
- meta2.Attributes.Add("content", "blah");
- meta3.Attributes.Add("name", "abstract");
- meta3.Attributes.Add("content", "blah");
- // Add the meta object to the htmlhead's control collection
- head.Controls.Add(meta);
- head.Controls.Add(meta2);
- head.Controls.Add(meta3);
- }
- }
- }
- HeaderProvider.cs
- namespace TMG.DAL
- {
- public abstract class HeaderProvider : DataAccess
- {
- //public abstract HeaderDetails GetHeaderByID(string pageId);
- protected virtual HeaderDetails GetHeaderFromReader(IDataReader reader)
- {
- return GetHeaderFromReader(reader, true);
- }
- protected virtual HeaderDetails GetHeaderFromReader(IDataReader reader, bool readBody)
- {
- HeaderDetails headers = new HeaderDetails(
- (int)reader["id"],
- null,
- null,
- null,
- reader["javascript"].ToString(),
- reader["title"].ToString(),
- reader["css"].ToString(),
- reader["page_id"].ToString());
- if (readBody)
- headers.AbstractText = reader["abstractText"].ToString();
- headers.Description = reader["description"].ToString();
- headers.Keywords = reader["keywords"].ToString();
- return headers;
- }
- }
- }
- HeaderDetails.cs
- namespace TMG.DAL
- {
- public class HeaderDetails
- {
- public HeaderDetails(int id, string abstractText, string description, string keywords, string javascript, string title, string css, string page_id)
- {
- this.ID = id;
- this.AbstractText = abstractText;
- this.Description = description;
- this.Keywords = keywords;
- this.Javascript = javascript;
- this.Title = title;
- this.Css = css;
- this.PageId = page_id;
- }
- private int _id = 0;
- public int ID
- {
- get { return _id; }
- set { _id = value; }
- }
- private string _abstractText = "";
- public string AbstractText
- {
- get { return _abstractText; }
- set { _abstractText = value; }
- }
- private string _description = "";
- public string Description
- {
- get { return _description; }
- set { _description = value; }
- }
- private string _keywords = "";
- public string Keywords
- {
- get { return _keywords; }
- set { _keywords = value; }
- }
- private string _javascript = "";
- public string Javascript
- {
- get { return _javascript; }
- set { _javascript = value; }
- }
- private string _title = "";
- public string Title
- {
- get { return _title; }
- set { _title = value; }
- }
- private string _css = "";
- public string Css
- {
- get { return _css; }
- set { _css = value; }
- }
- private string _pageId = "";
- public string PageId
- {
- get { return _pageId; }
- set { _pageId = value; }
- }
- }
- }
- GetHeader.cs
- namespace TMG.DAL
- {
- public class GetHeader : HeaderProvider
- {
- private int _id = 0;
- public int ID
- {
- get { return _id; }
- set { _id = value; }
- }
- private string _abstractText = "";
- public string AbstractText
- {
- get { return _abstractText; }
- set { _abstractText = value; }
- }
- private string _description = "";
- public string Description
- {
- get { return _description; }
- set { _description = value; }
- }
- private string _keywords = "";
- public string Keywords
- {
- get { return _keywords; }
- set { _keywords = value; }
- }
- private string _javascript = "";
- public string Javascript
- {
- get { return _javascript; }
- set { _javascript = value; }
- }
- private string _title = "";
- public string Title
- {
- get { return _title; }
- set { _title = value; }
- }
- private string _css = "";
- public string Css
- {
- get { return _css; }
- set { _css = value; }
- }
- private string _pageId = "";
- public string PageId
- {
- get { return _pageId; }
- set { _pageId = value; }
- }
- public GetHeader(int id, string abstractText, string description, string keywords, string javascript, string title, string css, string page_id)
- {
- this.ID = id;
- this.AbstractText = abstractText;
- this.Description = description;
- this.Keywords = keywords;
- this.Javascript = javascript;
- this.Title = title;
- this.Css = css;
- this.PageId = page_id;
- }
- public virtual HeaderDetails GetHeaderById(string pageId)
- {
- SqlConnection cn = GetConnection();
- SqlCommand oCommand = new SqlCommand("page_header",cn);
- oCommand.CommandType=CommandType.StoredProcedure; // Parametrs
- oCommand.Parameters.Add("@pageId", SqlDbType.VarChar).Value = pageId;
- cn.Open();
- IDataReader reader = ExecuteReader(oCommand, CommandBehavior.SingleRow);
- if (reader.Read())
- return GetHeaderFromReader(reader, true);
- else
- return null;
- }
- }
- }
- DataAccess.cs
- namespace TMG.DAL
- {
- public abstract class DataAccess
- {
- protected static string strConnect;
- public DataAccess()
- {
- }
- static DataAccess()
- {
- strConnect = ConfigurationManager.AppSettings["connstr"];
- }
- protected SqlConnection GetConnection()
- {
- SqlConnection oConnection = new SqlConnection(strConnect);
- return oConnection;
- }
- protected IDataReader ExecuteReader(DbCommand cmd)
- {
- return ExecuteReader(cmd, CommandBehavior.Default);
- }
- protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior)
- {
- return cmd.ExecuteReader(behavior);
- }
- protected object ExecuteScalar(DbCommand cmd)
- {
- return cmd.ExecuteScalar();
- }
- }
- }