Hi I wonder if anyone can help me.
I am trying to create a pageBase class where I can dynamically pull data from the database and display it on a web page. Each page I specifically set an ID for the sql query which pulls back the information such as page title and page text.
my problem is that I can’t seem to pass the information captured from the query to the page. I get problems such as can’t convert string type to literal and I need to declare a new keyword to an object.
PageBase Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
public class PageBase : Page
{
public int PageId { get; set; }
protected Literal contentHeader;
protected Literal contentText;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString =
ConfigurationManager.ConnectionStrings["skylightstudios"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT m_name, m_text, m_id FROM pages WHERE m_id = " + PageId, conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
if (reader.Read())
{
contentHeader.Text = "<h1>" + Server.HtmlEncode(reader["m_name"].ToString()) + "</h1>";
contentText.Text = Server.HtmlEncode(reader["m_text"].ToString());
}
reader.Close();
}
finally
{
conn.Close();
}
}
}
My Code Behind on page
public partial class _Default : PageBase
{
protected override void OnInit(EventArgs e)
{
this.PageId = 2;
this.contentHeader = this.ltlHeader;
this.contentText = this.ltlContent;
base.OnInit(e);
}
}
my page
<asp:Literal runat="server" id="ltlHeader" />
<div><p><asp:Literal runat="server" id="ltlContent" /></p></div>
I think it something to do with passing a string value but even if I just pass the value it gives an error.
can someone help or give me advise? btw I am still learning asp.net.