I want to learn how to create a blog. I created 3 webform to take care of this. The first webform is used to insert data into the database, the second web form is used to display the Titles and the third webform is to display the Titles, and contents of the blog. I named the webforms (addblog.aspx, blogTitles.aspx, and blogcontents.aspx).
I am not going into the part where I insert the data because that is working.
I am having issue with the part where I click on a blog Title to redirect to the page where the contents will be displayed, but I get an error that states:
"The resource you are looking for has been removed, had its name changed ..........". I don't know if I did not get to write the routing well in the Global.asax file. Please help me check. Thanks
This is the HTML Markup for blogTitles.aspx where blog Titles will be displayed. Here, when you click on the Hyperlink used to display the titles it should redirect and display the contents
<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("~/contents/{0}/{1}.aspx", Eval("ArticleId"), Eval("Slug")) %>'
Text='<%# Eval("Title") %>' runat="server" />
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
</div>
This is the code that displays the blog Titles from database table
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]";
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True"))
{
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();
}
}
}
}
}
This is the page where blog contents (i.e. body) will be displayed.
The HTML markup
<div class="col-sm-10" style="margin: 0 auto; padding: 6px;">
<h5>
<asp:Label ID="lblTitle" runat="server" Text="Title" />
<%--<asp:Label ID="timelbl" runat="server" Text="" Font-Size="Smaller" Font-Bold="true"></asp:Label>--%>
</h5>
<hr />
<asp:Label ID="lblBody" runat="server" Text="Body" />
</div>
This is the code that is supposed to display the contents
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateArticle();
}
}
private void PopulateArticle()
{
string articleId = this.Page.RouteData.Values["ArticleId"].ToString();
string query = "SELECT [Title], [Body] FROM [Articles] WHERE [ArticleId] = @ArticleId";
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True"))
{
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 = dt.Rows[0]["DatePosted"].ToString();
}
}
}
}
}
I also did routing using the Global.asax file
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
ScriptManager.ScriptResourceMapping.AddDefinition("jquery",
new ScriptResourceDefinition
{
Path = "~/scripts/jquery-1.7.2.min.js",
DebugPath = "~/scripts/jquery-1.7.2.min.js",
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js",
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.js"
});
RegisterRoutes(RouteTable.Routes);
}
static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Default", "home", "~/Default.aspx");
routes.MapPageRoute("LoginForm", "login", "~/LoginForm.aspx");
routes.MapPageRoute("SignUp", "signup", "~/SignUp.aspx");
routes.MapPageRoute("generalsupport", "general-support", "~/generalsupport.aspx");
routes.MapPageRoute("overview", "dashboard", "~/overview.aspx");
routes.MapPageRoute("PasswordRecovery", "RecoverPassword", "~/PasswordRecovery.aspx");
routes.MapPageRoute("blogcontents.aspx", "Articles/{ArticleId}/{Slug}.aspx", "blogcontents.aspx");
}