locked
How do I select Id of a post in datalist by button click RRS feed

  • Question

  • User-2074858223 posted
    Hi please any idea on How to select ID of a post in datalist by button click
    Thursday, July 14, 2016 11:20 AM

Answers

  • User475983607 posted

    Well I gave my procedure and the two tables with data.

    The code and schema have too many errors to provide a solid answer.

    So How do I fetch and display the comments on Same datalist displaying userpost?

    My best guess is you want to fetch a post and related comments by post id.  Or maybe by original poster username? It hard to tell because you've asked 3 different questions through out this thread. 

    Here's my best guess...  the first query returns posts and comments by PostId. The second returns Posts and Comments by the original poster's username. The third fetches posts by the original poster's UserId.

    IF OBJECT_ID('tempdb..#USERPost') IS NOT NULL
    	DROP TABLE #USERPost
    
    IF OBJECT_ID('tempdb..#UserCommentPost') IS NOT NULL
    	DROP TABLE #UserCommentPost
    
    IF OBJECT_ID('tempdb..#User3') IS NOT NULL
    	DROP TABLE #User3
    
    
    CREATE TABLE #USERPost (
    	PostId			INT IDENTITY(1,1),
    	UserName		VARCHAR(50),
    	FriendUserName	VARCHAR(50),
    	ContentPost		VARCHAR(MAX),
    	SendDate		DATETIME
    )
    
    
    CREATE TABLE #UserCommentPost (
    	CommentId		INT IDENTITY(1,1),
    	PostId			INT,
    	UserName		VARCHAR(50),
    	FriendUserName	VARCHAR(50),
    	Comments		VARCHAR(MAX),
    	SendDate		DATETIME
    )
    
    CREATE TABLE #User3 (
    	User3Id			INT IDENTITY(1,1),
    	UserName		VARCHAR(50),
    )
    
    INSERT INTO #USERPost (
    	UserName,
    	FriendUserName,
    	ContentPost,
    	SendDate
    )
    VALUES	('micah22', null, 'Post 1', GETDATE()),
    		('micah22', null, 'Post 2', GETDATE()),
    		('kelin', null, 'Post 3', GETDATE())
    
    
    INSERT INTO #UserCommentPost (
    	PostId,
    	UserName,
    	FriendUserName,
    	Comments,
    	SendDate
    	)
    VALUES  (2, 'micah22', 'micah22', 'comment 1', GETDATE()),
    		(2, 'kelin', 'micah22', 'comment 2', GETDATE()),
    		(1, 'kelin', 'micah22', 'comment 3', GETDATE()),
    		(1, 'kelin', 'micah22', 'comment 4', GETDATE()),
    		(3, 'micah22', 'kelin', 'comment 5', GETDATE()),
    		(3, 'micah22', 'kelin', 'comment 6', GETDATE())
    
    
    
    INSERT INTO #User3 (
    	UserName
    )
    VALUES ('micah22'), ('kelin')
    
    
    SELECT	u1.User3Id AS [Origianl Poster ID],
    		up.UserName AS [Origianl Poster],
    		up.ContentPost AS [Origianl Post],
    		u2.User3Id AS [Comment Poster ID],
    		uc.UserName AS [Comment Poster],
    		uc.Comments
    FROM #USERPost AS up
    	INNER JOIN #UserCommentPost AS uc ON up.PostId = uc.PostId
    	INNER JOIN #User3 AS u1 ON u1.UserName = up.UserName
    	INNER JOIN #User3 AS u2 ON u2.UserName = uc.UserName
    WHERE up.PostId = 3
    
    
    SELECT	u1.User3Id AS [Origianl Poster ID],
    		up.UserName AS [Origianl Poster],
    		up.ContentPost AS [Origianl Post],
    		u2.User3Id AS [Comment Poster ID],
    		uc.UserName AS [Comment Poster],
    		uc.Comments
    FROM #USERPost AS up
    	INNER JOIN #UserCommentPost AS uc ON up.PostId = uc.PostId
    	INNER JOIN #User3 AS u1 ON u1.UserName = up.UserName
    	INNER JOIN #User3 AS u2 ON u2.UserName = uc.UserName
    WHERE up.UserName = 'micah22'
    
    SELECT	u1.User3Id AS [Origianl Poster ID],
    		up.UserName AS [Origianl Poster],
    		up.ContentPost AS [Origianl Post],
    		u2.User3Id AS [Comment Poster ID],
    		uc.UserName AS [Comment Poster],
    		uc.Comments
    FROM #USERPost AS up
    	INNER JOIN #UserCommentPost AS uc ON up.PostId = uc.PostId
    	INNER JOIN #User3 AS u1 ON u1.UserName = up.UserName
    	INNER JOIN #User3 AS u2 ON u2.UserName = uc.UserName
    WHERE u1.User3Id = 2
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 17, 2016 4:06 PM

All replies

  • User2103319870 posted

    How to select ID of a post in datalist by button click

    If the button is inside the datalist then you can find the id of selected button like below

    /// <summary>
            /// Handles the Click event of the Button1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
            protected void Button1_Click(object sender, EventArgs e)
            {
                //Get the Button from sender
                Button btn = sender as Button;
                //Get the current datalistitem
                DataListItem dtl = btn.NamingContainer as DataListItem;
                //Find the label control which contains id values
                Label lbl = dtl.FindControl("label1") as Label;
                string id = lbl.Text;
    
            }

    Complete Code

    HTML

     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorks2008R2ConnectionString2 %>"
                    SelectCommand="SELECT top 20  [AddressID], [AddressLine1],City,StateProvinceID,PostalCode,ModifiedDate FROM Person.Address"></asp:SqlDataSource>
    
                <asp:DataList ID="DataList1" runat="server" RepeatColumns="2" CellSpacing="5" DataSourceID="SqlDataSource2"
                    RepeatDirection="Horizontal" RepeatLayout="Table">
                    <ItemTemplate>
                        <div class="item">
                            <asp:Label runat="server" ID="label1" Text='<%# DataBinder.Eval(Container.DataItem, "AddressID" ) %>'></asp:Label>
                            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                        </div>
                    </ItemTemplate>
                </asp:DataList>

    C#:

    /// <summary>
            /// Handles the Click event of the Button1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
            protected void Button1_Click(object sender, EventArgs e)
            {
                //Get the Button from sender
                Button btn = sender as Button;
                //Get the current datalistitem
                DataListItem dtl = btn.NamingContainer as DataListItem;
                //Find the label control which contains id values
                Label lbl = dtl.FindControl("label1") as Label;
                string id = lbl.Text;
    
            }

    Thursday, July 14, 2016 11:27 PM
  • User-2074858223 posted

    This is what i did and i didnt get answer

     protected void btncomments_Click(object sender, EventArgs e)
        {
            int userid = 0;
            //Get the Button from sender
            Button btn = sender as Button;
            //Get the current datalistitem
            DataListItem dtl = btn.NamingContainer as DataListItem;
            //Find the label control which contains id values
            Label lbl = dtl.FindControl("lblname") as Label;
            string id = lbl.Text;
             SqlDataAdapter adp = new SqlDataAdapter("GetUSERcomment", con);
            adp.SelectCommand.CommandType = CommandType.StoredProcedure;
          //  adp.SelectCommand.Parameters.AddWithValue("@MyComments", userid);
            adp.SelectCommand.Parameters.AddWithValue("@Id", userid);
            // adp.SelectCommand.Parameters.AddWithValue("@Id", id);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
            {
                tblcomments.DataSource = dt;
                tblcomments.DataBind();
        }

    the procedure

    ALTER PROCEDURE [dbo].[GetUSERcomment]
    	
    	
    	 @UserName VARCHAR(200)
    	
    	
    AS
    BEGIN
    SELECT k.UserId,k.ImageName,k.Name,v.Id,s.Id,s.FriendUserName,s.Id,s.postId,s.SendDate,s.UserName,s.MyComments  FROM UserCommentPost s, USERPost v, User3 k WHERE  s.Id=s.Id AND v.UserName=@UserName  ORDER BY s.postId DESC
    
    
    
    END
    
    


    the issue with my challenges is this, i have a datalist holding post made by users, and the post was inserted using usernames.

     Now i have  the datalist that is showing all these posts and their is a textbox comment and a button submit to allow users to make comment on the posts in the datalist. here is the challenge, the comment has to be inserted using userID not username that means that when a user makes a comment the UserId will be fetched and inserted in the comment table. I have succeded to insert the records using userID and it worked, my problem now is to select these comments inline with these posts.

    this image below is the comments in comment table using USERID which is postId

    here below is the table holding user posts

    so my question is how do i display the comments using the table ID

    Sunday, July 17, 2016 10:54 AM
  • User475983607 posted

    here is the challenge, the comment has to be inserted using userID not username that means that when a user makes a comment the UserId will be fetched and inserted in the comment table.

    How is this possible when comment table schema does not contains a UserId?

    Please don't take this criticism badly but the code is so poorly written and contains so many errors that it is impossible to understand what you're trying to do.  

    Sunday, July 17, 2016 1:49 PM
  • User-2074858223 posted
    Well I gave my procedure and the two tables with data. I explained what I wanted to achieve. First I have a table holding user posts and secondly I have a table holding user comments. The primary key is ID while foreign key in user comment table is postId. On the user post table users made post with usernames, but to make comment on each of the post, the user made comments using the ID which is inserted into user post table as postId. So How do I fetch and display the comments on Same datalist displaying userpost?
    Sunday, July 17, 2016 3:10 PM
  • User475983607 posted

    Well I gave my procedure and the two tables with data.

    The code and schema have too many errors to provide a solid answer.

    So How do I fetch and display the comments on Same datalist displaying userpost?

    My best guess is you want to fetch a post and related comments by post id.  Or maybe by original poster username? It hard to tell because you've asked 3 different questions through out this thread. 

    Here's my best guess...  the first query returns posts and comments by PostId. The second returns Posts and Comments by the original poster's username. The third fetches posts by the original poster's UserId.

    IF OBJECT_ID('tempdb..#USERPost') IS NOT NULL
    	DROP TABLE #USERPost
    
    IF OBJECT_ID('tempdb..#UserCommentPost') IS NOT NULL
    	DROP TABLE #UserCommentPost
    
    IF OBJECT_ID('tempdb..#User3') IS NOT NULL
    	DROP TABLE #User3
    
    
    CREATE TABLE #USERPost (
    	PostId			INT IDENTITY(1,1),
    	UserName		VARCHAR(50),
    	FriendUserName	VARCHAR(50),
    	ContentPost		VARCHAR(MAX),
    	SendDate		DATETIME
    )
    
    
    CREATE TABLE #UserCommentPost (
    	CommentId		INT IDENTITY(1,1),
    	PostId			INT,
    	UserName		VARCHAR(50),
    	FriendUserName	VARCHAR(50),
    	Comments		VARCHAR(MAX),
    	SendDate		DATETIME
    )
    
    CREATE TABLE #User3 (
    	User3Id			INT IDENTITY(1,1),
    	UserName		VARCHAR(50),
    )
    
    INSERT INTO #USERPost (
    	UserName,
    	FriendUserName,
    	ContentPost,
    	SendDate
    )
    VALUES	('micah22', null, 'Post 1', GETDATE()),
    		('micah22', null, 'Post 2', GETDATE()),
    		('kelin', null, 'Post 3', GETDATE())
    
    
    INSERT INTO #UserCommentPost (
    	PostId,
    	UserName,
    	FriendUserName,
    	Comments,
    	SendDate
    	)
    VALUES  (2, 'micah22', 'micah22', 'comment 1', GETDATE()),
    		(2, 'kelin', 'micah22', 'comment 2', GETDATE()),
    		(1, 'kelin', 'micah22', 'comment 3', GETDATE()),
    		(1, 'kelin', 'micah22', 'comment 4', GETDATE()),
    		(3, 'micah22', 'kelin', 'comment 5', GETDATE()),
    		(3, 'micah22', 'kelin', 'comment 6', GETDATE())
    
    
    
    INSERT INTO #User3 (
    	UserName
    )
    VALUES ('micah22'), ('kelin')
    
    
    SELECT	u1.User3Id AS [Origianl Poster ID],
    		up.UserName AS [Origianl Poster],
    		up.ContentPost AS [Origianl Post],
    		u2.User3Id AS [Comment Poster ID],
    		uc.UserName AS [Comment Poster],
    		uc.Comments
    FROM #USERPost AS up
    	INNER JOIN #UserCommentPost AS uc ON up.PostId = uc.PostId
    	INNER JOIN #User3 AS u1 ON u1.UserName = up.UserName
    	INNER JOIN #User3 AS u2 ON u2.UserName = uc.UserName
    WHERE up.PostId = 3
    
    
    SELECT	u1.User3Id AS [Origianl Poster ID],
    		up.UserName AS [Origianl Poster],
    		up.ContentPost AS [Origianl Post],
    		u2.User3Id AS [Comment Poster ID],
    		uc.UserName AS [Comment Poster],
    		uc.Comments
    FROM #USERPost AS up
    	INNER JOIN #UserCommentPost AS uc ON up.PostId = uc.PostId
    	INNER JOIN #User3 AS u1 ON u1.UserName = up.UserName
    	INNER JOIN #User3 AS u2 ON u2.UserName = uc.UserName
    WHERE up.UserName = 'micah22'
    
    SELECT	u1.User3Id AS [Origianl Poster ID],
    		up.UserName AS [Origianl Poster],
    		up.ContentPost AS [Origianl Post],
    		u2.User3Id AS [Comment Poster ID],
    		uc.UserName AS [Comment Poster],
    		uc.Comments
    FROM #USERPost AS up
    	INNER JOIN #UserCommentPost AS uc ON up.PostId = uc.PostId
    	INNER JOIN #User3 AS u1 ON u1.UserName = up.UserName
    	INNER JOIN #User3 AS u2 ON u2.UserName = uc.UserName
    WHERE u1.User3Id = 2
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 17, 2016 4:06 PM