I created an article in my website and everything worked perfectly. But when I created a new article in web application, I got an error that says:
Object reference not set to an instance of an object.
And this error means that an object that is being called to get its value has no reference or the reference is null.
But in the table, I got this
ArticleId (int)
|
Title(nvarchar(MAX)
|
Body(nvarchar(MAX)
|
DatePosted(datetime)
|
Type (nvarchar(MAX)
|
1
|
How to get strated
|
Getting started is easy……..
|
21-Feb-23 3:04:31 AM
|
Help Topic
|
I don’t know how this error is happens because the reference points to the ArticleId, which is not empty
HTML displaying articles Title
<div class="row col-sm-11" style="width: 100%; margin: 0 auto; padding: 10px; margin-top: 5%; margin-bottom: 7%;">
<div class="col-sm-10" style="margin: 0 auto; padding: 6px;">
<asp:Repeater ID="rptPages" runat="server">
<ItemTemplate>
<%# Container.ItemIndex + 1 %>.
<asp:HyperLink ID="HyperLink2" NavigateUrl='<%# string.Format("~/topic.aspx/{0}/{1}", Eval("ArticleId"), Eval("Slug")) %>' Text='<%# Eval("Title") %>' runat="server" Target="_blank" />
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
</div>
</div>
HTML display article Titles and Body
<!--This article Title is displaying as a sidebar navigation--->
<div class="top">
<nav id="sidebar" style="font-family: 'Graphik', sans-serif;">
<asp:Repeater ID="rptPages" runat="server">
<ItemTemplate>
<%# Container.ItemIndex + 1 %>.
<asp:HyperLink ID="HyperLink2" NavigateUrl='<%# string.Format("~/topic.aspx/{0}/{1}", Eval("ArticleId"), Eval("Slug")) %>' Text='<%# Eval("Title") %>' runat="server"/>
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
</nav>
</div>
<!--This article Title is displaying as a sidebar navigation--->
<!--- This is where the body/content of the article is displayed--->
<div class="col-sm-11" style="width: 100%; margin: 0 auto; color: #011b33; padding: 10px;">
<div class="col-sm-10" style="margin: 0 auto; padding: 10px; width: 100%;">
<label style="color: #00003D;">Detailed contents</label>
</div>
<div class="col-sm-10" style="margin: 0 auto; padding: 6px;">
<h5>
<asp:Label ID="lblTitle" runat="server" Text="Title" />
<label style="font-size: 7pt; font-family: Nunito;">posted on: </label>
<asp:Label ID="timelbl" runat="server"></asp:Label>
</h5>
<hr />
<asp:Label ID="lblBody" runat="server" Text="Body" />
</div>
</div>
<!--- This is where the body/content of the article is displayed--->
C# for article Title
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateArticleId();
}
}
private void PopulateArticleId()
{
string query = "SELECT [ArticleId], [Title], REPLACE([Title], ' ', '-') [SLUG], [Body] FROM [Articles]";
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
rptPages.DataSource = dt;
rptPages.DataBind();
}
}
}
}
}
C# for article title and body
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateArticle();
this.PopulateArticleID();
}
}
private void PopulateArticleID()
{
string query = "SELECT [ArticleId], [Title], REPLACE([Title], ' ', '-') [SLUG], [Body] FROM [Articles]";
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
rptPages.DataSource = dt;
rptPages.DataBind();
}
}
}
}
}
private void PopulateArticle()
{
string articleId = this.Page.RouteData.Values["ArticleId"].ToString();
string query = "SELECT [Title], [Body], [DatePosted] FROM [Articles] WHERE [ArticleId] = @ArticleId";
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@ArticleId", articleId);
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
lblTitle.Text = dt.Rows[0]["Title"].ToString();
lblBody.Text = dt.Rows[0]["Body"].ToString();
timelbl.Text = Convert.ToDateTime(dt.Rows[0]["DatePosted"]).ToString("MMMM dd, yyyy h:mm tt") + ".";
}
}
}
}
}
Global.asax file MapPageRoute
routes.MapPageRoute("topic", "topic.aspx/{ArticleId}/{Slug}", "~/topic.aspx");