Creating posts and comments similar to Facebook - asp.net

I need my own posts and comment on the page in my project, something similar to Facebook. I succesfully created posts by using sql table, sqldatasource and repeater. But I have a problem with comments.
I created sql table Comments, there is a column with Post ID. This is my code:
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:MotWebConnectionString %>"
SelectCommand="SELECT * FROM [Posts]
JOIN [Users] ON [Users].[UserName] = [Posts].[UserName]
JOIN [Comments] ON [Comments].[PostId] = [Posts].[PostId]
WHERE ([GroupId] = #GroupId) ">
<SelectParameters>
<asp:QueryStringParameter Name="GroupId" QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource3" >
<ItemTemplate>
<table style="width: 100%;">
<tr>
<td width="10%">
<asp:Image ID="Image2" runat="server" Width="64px" Height="64px" ImageUrl='<%# Eval("ProfilePicture")%>' />
</td>
<td align="left" width="90%" >
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("UserName", "~/_/Profile.aspx?Username={0}")%>'><p><asp:Literal ID="Literal2" runat="server" Text='<%# Eval("Username")%>'></asp:Literal></p></asp:HyperLink>
</td>
</tr>
</table>
<asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Text")%>'></asp:Literal>
<br />
<asp:Literal ID="Literal4" runat="server" Text='<%# Eval("CommentText")%>'></asp:Literal>
<asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource4" DefaultMode="Insert" >
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="Insert" />
</InsertItemTemplate>
</asp:FormView>
</ItemTemplate>
<SeparatorTemplate>
<br />
<br />
<br />
</SeparatorTemplate>
</asp:Repeater>
But it shows only posts, where is at least 1 comment and if there are more comments than 1, the posts is twice or more times on the page.
And another problem is, that I don't know how to insert right Post ID into the Comments table. It is probably by using Parameter...
My database schema (I did't know how to create it in SQL Management Studio, so it is in VS):
http://img685.imageshack.us/img685/1416/vstiekd.jpg
Please could anyone help me with this:
How to view right comments under the post and view also posts without any comment
How to insert the comment with right Post ID
I will be very thankfull for any kind of help

Related

ASP.net get form values from postback inline vb.net

This codes works great except for getting the post values... I have the select hard coded now, but I want to obtain the values from the form post and populate from that... I hope there's a way to do this using inline asp.net (vb.net)...
<%# Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView Templates Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Modifying Quantities for: </h3>
<asp:SqlDataSource ID="Sql" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [name],[id] FROM [markets]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="yearSQLDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT distinct(year) as year from transactions order by year asc">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT shortname, id from products order by name asc">
</asp:SqlDataSource>
<asp:Label ID="market" runat="server" Text="Market:" />
<asp:DropDownList ID="markets" runat="server"
DataSourceID="Sql" DataTextField="name" DataValueField="id" >
</asp:DropDownList>
<asp:Label ID="Label2" runat="server" Text="Products:" />
<asp:DropDownList ID="products" runat="server"
DataSourceID="SqlDataSource1" DataTextField="shortname" DataValueField="id" >
</asp:DropDownList>
<!--TODO use databind in Page_Load to fill from SQL and then use distributor ID instead of name to match-->
<asp:Label ID="year" runat="server" Text="Year:" />
<asp:DropDownList ID="years" runat="server"
DataSourceID="yearSQLDataSource" DataTextField="year" DataValueField="year" >
</asp:DropDownList>
<asp:button ID="Button1" runat="server" text="Submit" postbackurl="fourrosesformDataList3.aspx" />
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="id"
runat="server">
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" runat="server" id="tblProducts">
<tr id="Tr1" runat="server">
<th id="Th1" runat="server">Action</th>
<th id="Th2" runat="server">Month</th>
<th id="Th3" runat="server">Quantity</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr2" runat="server">
<td>
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="Label1" runat="Server" Text='<%#Eval("MonthName") %>' />
</td>
<td>
<asp:Label DataTextField = "standardcase" ID="standardcase" runat="Server" Text='<%#Eval("standardcase") %>' />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr style="background-color: #ADD8E6">
<td>
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="Label3" runat="Server" Text='<%#Eval("MonthName") %>' />
</td>
<td>
<asp:TextBox ID="standardcase" DataTextField = "standardcase" runat="server" Text='<%# Bind("standardcase") %>'
MaxLength="50" /><br />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT standardcase, shortname, datename(month,dateadd(month, t.month - 1, 0)) as MonthName, t.id as id
from transactions t, products p where p.id=t.product_id and year = 2009 and market_id = 1 and product_id = 1"
UpdateCommand="UPDATE transactions
SET standardcase = #standardcase
WHERE id = #id"
>
</asp:SqlDataSource>
</form>
</body>
</html>
so instead of "where p.id=t.product_id and year = 2009 and market_id = 1 and product_id = 1"
I want to do "where p.id=t.product_id and year = #postYear and market_id = #postMarket and product_id = #postProduct" from the form at the top of the page.
Use the selectparameters tag of asp:sqldatasource. Exactly how you do it depends on whether you are getting the value from a control on the current page.
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
selectcommand="SELECT standardcase, shortname, datename(month,dateadd(month, t.month - 1, 0)) as MonthName, t.id as id
from transactions t, products p where p.id=t.product_id and year = #year and market_id = #market and product_id = #product">
<selectparameters>
<asp:controlparameter name="year" controlid="DropDownList1" propertyname="SelectedValue"/>
<asp:controlparameter name="market" controlid="DropDownList1" propertyname="SelectedValue"/>
<asp:controlparameter name="products" controlid="DropDownList1" propertyname="SelectedValue"/>
</selectparameters>
</asp:sqldatasource>
Be aware that this does leave a vulnerability, see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selectparameters(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 for more details.
The formparamter property worked for me:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formparameter.formfield(v=vs.110).aspx
I may look at the controlparameter too... T

Cannot access Ajax Rating from code behind

I have this html markup:
<div id="profil_rating_container">
<div id="profil_rating">
<div id="pr">
<h2 id="rating" class="profil_rating_title">Hodnocení <asp:Label ID="lblAddRating" runat="server"><span><i class="fa fa-plus-circle"></i></span></asp:Label></h2>
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:Rating ID="FinalRating" runat="server" MaxRating="5" ReadOnly="true" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar" FilledStarCssClass="mainFilledRatingStar" EmptyStarCssClass="mainEmptyRatingStar" />
<br />
<asp:SqlDataSource ID="RatinSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SportovniPlanetaCS %>"
SelectCommand="SELECT [RatingText], [RatingCount], [Name] FROM [rating] WHERE ([ProfileId] = #ProfileId) AND ([RatingText] IS NOT NULL)">
<SelectParameters>
<asp:QueryStringParameter Name="ProfileId" QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="RatingRepeater" DataSourceID="RatinSqlDataSource" runat="server" OnItemDataBound="RatingRepeater_ItemDataBound">
<ItemTemplate>
<h4><asp:Literal ID="RatingName" runat="server" Text='<%# Eval("Name") %>' /></h4>
<p class="p_rating"><asp:Literal ID="RatingText" runat="server" Text='<%# Eval("RatingText") %>'></asp:Literal></p>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblRating" runat="server" Text="Uživatel zatím nemá žádné hodnocení." Visible="false"></asp:Label>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</div>
And when I am accesing Ajax Control Toolkit Rating from code behind like this: FinalRating.CurrentRating = cmdRating.ExecuteScalar it underline FinalRating with error text: 'Final Rating' is not declared. It may be inaccessible due to its protection level.
I don't know why this is happening. I have the same code in other 5 pages and it is working finec, but in this one page it isn't. Also when I add new page and add Ajax Rating, I cannot access it from code behind. The rating control isn't in any data component like repeater or datalist. Can this be solved by FindControl? If yes, how exactly?
Does anyone know, what is causing this problem? Thanks for answer.

Changing AccessDataSource to SqlDataSource

I'm trying to change my database from Access to MS SQL and I've got the database ready and everything. I've gone around my website and changed the IDs for each data source to SqlDataSource(number here) and then changed each control that uses the property "DataSourceID" to reflect the name change. However, when I try to open my website, it says it is still looking for AccessDataSource1. Here is some of my code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TravelJoansDBSQLConnectionString %>"
SelectCommand="SELECT TOP 8 * FROM [Table2]"></asp:SqlDataSource>
<asp:DataList ID="DataList1" DataSourceID="SqlDataSource1"
runat="server" ItemStyle-CssClass="picTableStyle"
DataKeyField="ID">
<ItemStyle></ItemStyle>
<ItemTemplate>
<br /><br />
<table>
<tr>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"~/Places.aspx?ID="+Eval("ID")%>'>
<asp:Image ID="Image1" runat="server" BorderColor="#CCCCCC" BorderWidth="1px" Width="220px" Height="180px" ImageUrl='<%# "PlaceImages/" + Eval("Image") %>' /><br /></asp:HyperLink>
<asp:Label ID="Label1" CssClass="picCaptionStyle" runat="server" Text='<%# Eval("Caption") %>' /></td>
<td>
<asp:HyperLink runat="server" NavigateUrl='<%#"~/Places.aspx?ID="+Eval("ID")%>'>
<asp:Label ID="Label2" class="picTitleStyle" runat="server" Text='<%# Eval("BlogTitle") %>' /><br /></asp:HyperLink>
<asp:Label ID="Label3" class="picBodyText" runat="server" Text='<%# Eval("TravelDate", "{0:MMMMMMMMM dd, yyyy}") %>' /><br /><br />
<asp:Label ID="Label4" class="picBodyText" runat="server" Text='<%# Limit(Eval("PicText"), 400)+"..." %>' />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%#"~/Places.aspx?ID="+Eval("ID")%>' Text="[Read More]" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Again, when I try to run my website based on this code, I get the error "The DataSourceID of 'DataList1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource1' could not be found." Any help would be much appreciated.
Try a clean then rebuild. You can also try a Ctrl+F in all files to see if "AccessDataSource1" is lingering somewhere.

Website tells me my query has a reserved word, although it works in access

The exact error is "The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect."
Here's the query it has to be referring to, since it happened after I added it.
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/TravelJoansDB.accdb"
SelectCommand="SELECT TOP 5 FROM Table2 ORDER BY RND([ID])" />
<asp:DataList ID="Datalist3" runat="server"
DataSourceID="AccessDataSource3"
DataKeyField="ID"
RepeatColumns="5"
RepeatDirection="Horizontal"
GridLines="Both"
ItemStyle-CssClass="picRandomStyle">
<ItemStyle />
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="RandomImage" runat="server" AlternateText="Another Blog Post"
ImageUrl='<%# "PlaceImages/" + Eval("Image") %>'
Width="150" Height="135px" />
</td>
</tr>
<tr>
<td>
<hr />
<asp:Label ID="RandomLabel" runat="server" CssClass="picTitleStyle" Text='<%# Eval("BlogTitle") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Try: SELECT TOP 5 * FROM Table2 ORDER BY RND([ID])

Pass parameter id to sql query using url routing

I'm a beginner asp.net developer
Trying to create my first simple news portal pages.
Here is what I have:
Database
Admin_News.aspx to add news into database
Default.aspx to display all news with the title linkable
NewsDetails.aspx to display the details by ID when they click on the title in Default.aspx
How I did That:
in Default.aspx I used this code:
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
<tr>
<td>
<b>title</b>
</td>
<td>
<b>news</b>
</td>
<td>
<b>imges</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink runat="server" ID="hl" NavigateUrl='<%#"~/NewsDetails.aspx?id=" + Eval("id")%>' Text='<%# Eval("title") %>'></asp:HyperLink>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "news")%>
</td>
<td>
<img alt="" src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MoneyHolderConnectionString %>"
SelectCommand="SELECT [id], [title], [news], [imageurl], [detail] FROM [News]"></asp:SqlDataSource>
</div>
NewsDetails.aspx code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MoneyHolderConnectionString %>"
SelectCommand="SELECT [title], [news], [imageurl], [detail] FROM [News] WHERE ([id] = #id)">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<h1 align="right"><br />
<strong><p style="color:#000">
<%# DataBinder.Eval(Container.DataItem, "title")%>
</strong></h1><p/><br />
<br />
<p align="center"><img src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>'/>
<p/>
<br />
<br />
<p id="detail" align="right" style="font-size:25px"><%# DataBinder.Eval(Container.DataItem, "detail")%><p/>
<br />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
so what I'm doing is displaying the article according to the ID in .aspx?id= in the url.
everything was working fine.. until I used the url routing.
I changed/added some codes to change the structure of the url I don't want it to appear like ~/NewsDetails.aspx?id=1
I want to be like ~/News/1 instead with the same result.
So I have added this code to Global.asax:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("RouteForNews", "News/{id}", "~/NewsDetails.aspx");
}
and I added this code to the NewsDetails.aspx.cs code behind:
string id = Page.RouteData.Values["id"].ToString();
and I changed the NavigateUrl in NewsDetails.aspx to:
NavigateUrl='<%#"~/News/" + Eval("id")%>'
now when I open Default.aspx the news appear with the titles linkable to ~/News/"id number", when i click on a title, NewsDetails.aspx opens with the link of /News/idnumber but no data inside..its empty i only can see the design of the master page.
I would appreciate any help from you what should I do to display the news, the id value in the url it goes to string id variable but I don't know how to pass it to the sql query (i'm not sure if this is the problem)
You should not use the asp:QueryStringParameter in the asp:SqlDataSource in the NewsDetails page, as your parameter is no longer in the QueryString. A QueryString is the part of URL that comes after the question mark.
Use this link to know how to pass parameters to select statement:
How to pass variable to SelectCommand of SqlDataSource?
connectionstring="<%$ ConnectionStrings:MyConnection %>"
selectcommand="SELECT Name, Age FROM ContactInfo where
State=#StateCode">

Resources