Click here to Skip to main content
15,068,678 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am tring to make wall post like facebook but there is few issue. whenewere i m comment on post so that comment show in al post

i am giving my backcode and front code

there i am using 2 list view to show post and then comment.


this my aspx page design code

XML
<div class="blog_Div">
    <asp:TextBox ID="TextBox1" placeholder="Write Your Comment.." class="blog"
        runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />


     <telerik:RadSearchBox ID="RadSearchBox1" class="TextBox2" runat="server" Width="400"
        DataSourceID="SqlDataSource1"  DataTextField="name"
            MaxResultCount="2" RenderMode="Lightweight">

            <DropDownSettings  Height="50">
           <HeaderTemplate>
                    <ul>
                        <li class="col1"></li>

                    </ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <ul>
                        <li class="col1">

                            <%# DataBinder.Eval(Container.DataItem, "name") %></li>
                    </ul>
                </ItemTemplate>

            </DropDownSettings>
             </telerik:RadSearchBox>
             <br />
    <asp:Button ID="Button2"  class="Button1" runat="server" Text="Assign" onclick="Button2_Click" />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label><asp:Label ID="Label2"
        runat="server" Text=""></asp:Label>
</div><br /><br />
<div>

</div>

<div class="postview" >


     <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
        ViewStateMode="Disabled"  OnItemDataBound="use"
        >
        <AlternatingItemTemplate><br>
        <div class="postback">
            <span>
            <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
            <span class="heding_color">Assigned to </span>
            <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'   />
            <br />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>' />
            <br />

            <br />


            <br />


            <br />
            <span class="heding_color">Posted by : </span>
            <asp:Label ID="asigntask_nameLabel" runat="server"
                Text='<%# Eval("asigntask_name") %>'  />



            <span class="asgndate "><span class="heding_color"> Posted On </span>
            <asp:Label ID="curnt_dateLabel" runat="server"
                Text='<%# Eval("curnt_date") %>'  /></span><br />





           <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>

                <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; ">
                <br />

                <asp:TextBox ID="TextBoxL"    placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br /> <br />

                    <asp:Button ID="Button3" CommandName="insert" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" /> </div> <br />

        </span></div>
        </AlternatingItemTemplate>


        <EditItemTemplate>
        <div class="postback">
            <span style="">
            <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'/>
             <span class="heding_color">Assigned to </span>
              <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'   />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:TextBox ID="messageTextBox" runat="server" Text='<%# Bind("message") %>' />
            <br />
            <br />

            <br />

            <br /><br />
           <span class="heding_color"> Posted by : </span>
              <asp:TextBox ID="asigntask_nameTextBox" runat="server"
                Text='<%# Bind("asigntask_name") %>' />



            <asp:TextBox ID="curnt_dateTextBox" runat="server"
                Text='<%# Bind("curnt_date") %>' />
            <br /><br />

         <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>
            <br />
                <div class="cust_summary replaybtn">Replay</div>

              <br />
                <div id="cust_details" class="cust_details" style="display:none; ">
                <br />

                <asp:TextBox ID="TextBoxL"  EnableViewState="true"   placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br /> <br />
                    <asp:Button ID="Button3" CommandName="insert" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" /> </div> <br />

            <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
                Text="Update" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                Text="Cancel" />
            <br />
            <br />
            </span></div>
        </EditItemTemplate>
        <EmptyDataTemplate>


            <span>No data was returned.</span>
        </EmptyDataTemplate>
     <InsertItemTemplate><br>
        <div class="postback">
            <span>
            <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
             <span class="heding_color">Assigned to </span>
             <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'   />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:TextBox ID="messageTextBox" runat="server" Text='<%# Bind("message") %>' />
            <br />
            <br />

            <br />

            <br />



            <br />
            <span class="heding_color"> Posted by : </span>
            <asp:TextBox ID="asigntask_nameTextBox" runat="server"
                Text='<%# Bind("asigntask_name") %>' />

            <asp:TextBox ID="curnt_dateTextBox" runat="server"
                Text='<%# Bind("curnt_date") %>' />
            <br /><br />

           <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>
                <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; ">
                <br />

                <asp:TextBox ID="TextBoxL"    placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br /> <br />
                    <asp:Button ID="Button3" CommandName="insert" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" /> </div> <br />

            <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
                Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                Text="Clear" />
            <br />
            <br />
            </span></div>
        </InsertItemTemplate>
    <ItemTemplate><br>
    <div class="postback">
        <span>
        <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
        <span class="heding_color">Assigned to </span>
         <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'  />
        <br /><br />
        <span class="heding_color">Description</span><br />
        <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>'   />
        <br />
        <br />

        <br />

        <br />




        <br />
        <span class="heding_color"> Posted by : </span>
        <asp:Label ID="asigntask_nameLabel" runat="server"
            Text='<%# Eval("asigntask_name") %>' />


        <span class="asgndate"><span class="heding_color"> Posted On </span>
        <asp:Label ID="curnt_dateLabel" runat="server"
            Text='<%# Eval("curnt_date") %>' /></span><br /><br />



        <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="coment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>
            <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; "><br />

                <asp:TextBox ID="TextBoxL" Enabled="true"   placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
                <br />
                    <asp:Button ID="Button3" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" />
                    </div>
        <br />


        </span></div>
    </ItemTemplate>
        <LayoutTemplate>


        <br>
            <div ID="itemPlaceholderContainer" runat="server">


                <span runat="server" id="itemPlaceholder" />
            </div>
            <div style="">
            </div>
        </LayoutTemplate>
        <SelectedItemTemplate>
        <div class="postback">
            <span >
            <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
            <span class="heding_color">Assigned to </span>
              <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'   />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>' />
            <br />
            <br />


            <br />

                   <span class="heding_color"> Posted by : </span>

            <asp:Label ID="asigntask_nameLabel" runat="server"
                Text='<%# Eval("asigntask_name") %>' />
            <span class="asgndate"><span class="heding_color">Posted On </span>
            <asp:Label ID="curnt_dateLabel" runat="server"
                Text='<%# Eval("curnt_date") %>' /></span><br /><br />


             <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>

                <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; "><br />

                <asp:TextBox ID="TextBoxL" placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
                <br />
                    <asp:Button ID="Button3" class="btncomment" runat="server" Text="Post" onclick="Button3_Click" />
                    </div>
            <br />
           <br />

            </span></div>
        </SelectedItemTemplate>


    </asp:ListView>


       <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
        SelectCommand="SELECT [message], [name], [curnt_date], [asigntask_name],[id] FROM [task] ORDER BY [id] DESC">


    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"


        SelectCommand="select task.id ,rply.name,rply.comment,rply.rpl_date from task join rply on task.id=rply.id where task.id=rply.id  ORDER BY task.id DESC
">
    </asp:SqlDataSource>



C#
string s2 = System.Web.HttpContext.Current.User.Identity.Name;

protected void Page_Load(object sender, EventArgs e)
{
   // string s2 = System.Web.HttpContext.Current.User.Identity.Name;

    Session["name"] = s2;

}

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
    string s2 = System.Web.HttpContext.Current.User.Identity.Name;

    SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);



   SqlCommand cmd = new SqlCommand("insert into task(message,name,curnt_date,asigntask_name) values('" + TextBox1.Text + "','" + RadSearchBox1.Text + "','" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "','" + s2 + "') ", a);

     a.Open();


       cmd.ExecuteNonQuery();
       a.Close();
       TextBox1.Text = "";
       RadSearchBox1.Text = "";

       this.Response.Redirect("TaskManager.aspx?user="+s2);
   }
    catch (System.Data.SqlClient.SqlException ex)
   {
       Label1.Text = ex.Message.ToString();

   }


}
protected void use(object sender, ListViewItemEventArgs e)
{
    Label id = (Label)e.Item.FindControl("id_Label");
    ViewState["id"] = id.Text;

}


protected void Button3_Click(object sender, EventArgs e)
{
    SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);



    int d = int.Parse((String)ViewState["id"]);
    var btn = (Button)sender;
   // if (btn.Text == "btnComent")
    //{

        var item = (ListViewItem)btn.NamingContainer;
        // find other controls:
        var btnModify = (TextBox)item.FindControl("TextBoxL");

        try
        {

            string s2 = System.Web.HttpContext.Current.User.Identity.Name;

            SqlCommand cmd = new SqlCommand("insert into rply(id,comment,name,rpl_date)values('"+d+"','" + btnModify.Text + "','" + s2 + "','" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "')", c);
            c.Open();
            cmd.ExecuteNonQuery();
            c.Close();
            btnModify.Text = "";
            Response.Redirect("TaskManager.aspx");

            /*
                     SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

                          SqlCommand cmd = new SqlCommand();


                          cmd.Connection = c; //assigning connection to command
                          cmd.CommandType = CommandType.Text; //representing type of command

                          cmd.CommandText ="update task set post=@post  where id=@id";


                          cmd.Parameters.AddWithValue("@post", btnModify.Text);
                          cmd.Parameters.AddWithValue("@id", d);
                          c.Open();
                          cmd.ExecuteNonQuery();
                          c.Close();
                          btnModify.Text = "";*/

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Label1.Text = ex.Message.ToString();
        }



    }
}



PLZ HELP ME TO GET OUT OF THIS ISSUE
Posted
Updated 10-Sep-14 1:41am
v2
Comments
ZurdoDev 10-Sep-14 7:42am
   
You'll need to debug this and find out where the issue is.
Aarti Yadav 10-Sep-14 7:54am
   
yes i does that but not getting that whenewere i comment so that comment is showing in all posts.
Aarti Yadav 10-Sep-14 8:06am
   
everything is working properly just comment is not showing only there we comment. is showing on all posts
Richard Deeming 10-Sep-14 8:06am
   
Your code is susceptible to SQL Injection[^].
Aarti Yadav 10-Sep-14 8:18am
   
in sql server 2008 r2 and in aps.net C# 2010
Richard Deeming 10-Sep-14 8:21am
   
The database and language version are irrelevant at this point. You're using string concatenation to build your queries, and that means anyone can hack your database.

Change your code to use parameterized queries. It's not particularly hard - you even have an example that you've commented out.
Aarti Yadav 10-Sep-14 8:39am
   
oky ...
Aarti Yadav 11-Sep-14 2:21am
   
still its showing in all posts...

1 solution

1. You use onitemdatabound to set the id in your ViewState. OnItemDataBound fires for each record. Therefore, ViewState will always contain the Id of the last task in the list.
2. From your insert query, it seems you are using an identity column in your database for the id of the task. Can you check if it is actually filled in the database?
3. Because if it is not filled correctly, or all tasks get the same id, then all replies will also get the same if, and the join will return all replies for all tasks.

So first check to see if the data in the database is actually correct (all tasks have a unique id, and all replies actually have the right task id stored with them). I have a strong feeling, based on the observations above, that your PK and FK fields are not filled correctly.

UPDATE: Check your select query for the replies:
SQL
select task.id ,rply.name,rply.comment,rply.rpl_date from task join rply on task.id=rply.id where task.id=rply.id  ORDER BY task.id DESC

How does that query limit the results at all. It will give you all replies for all tasks.

If you move the sqldatasource2 inside the itemtemplate of the outer listview, you can use a control parameter pointing to id_Label to filter the replies to one task.
   
v2
Comments
Aarti Yadav 11-Sep-14 2:28am
   
still its showing in all posts
kbrandwijk 11-Sep-14 5:22am
   
What a meaningless comment... Did you actually try anything I suggested? You're not answering any of my questions, so how do you expect me to help you at this point?
Aarti Yadav 11-Sep-14 5:40am
   
Tables PK and FK is are Filled Properly..

check my list view i think that the issue is there.
displaying a data is in wrong formate may be. plz check it out.
kbrandwijk 11-Sep-14 5:54am
   
I have updated my solution to include the next step in our troubleshooting. We have confirmed the data in the DB is correct, know we focus on how we retrieve the correct set of data. The select query is wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900