Button in Listview - Insert into Database

Hey

I recently asked, and receive a solution for, about nesting a listview within another listview.
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_25661420.html

Basically, I am creating a social networking site (for a university project). On the homepage, it displays recent postings and comments (each a separate listview, comment is inside postings).

What I am now trying to do is let users add comments to each post.

I have two issues. Firstly, it doesn't recognise the textbox element. I have declared it using the FindControl code, but when I click the button I get an error. The second is that it doesn't recognise the Post_ID of the posting.

I have attached the code the below. The insert code does work, as I've tested using actual values in the SQL Insert code (replacing @Post_ID and tb_addcomment).

I am using VB.net along with an Access Database (.mdb).

Thanks in advance.
Imports System.Data.OleDb
Imports System.Collections
Imports System.Web.UI
Imports System.Web
Imports System.Collections.Generic


Partial Class index
    Inherits System.Web.UI.Page
    Dim Post_ID As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'user must be logged in
        If Session("Email") = "" Then
            Response.Redirect("index.aspx")
        End If

        Dim thisuserid As Integer
        thisuserid = Session("User_ID")

        ads_friendrequests.SelectCommand = "SELECT * FROM [qryfriendrequests] WHERE ([User_ID] = " & thisuserid & " AND [Who_Added] <> " & thisuserid & ")"
        ads_recentmessages.SelectCommand = "(SELECT tblposts.User_ID, tblposts.Post_ID, tblposts.Post, tblposts.TimeStamp, " & _
            "tblusers.First_Name, tblusers.Surname, tblusers.Profile_Picture FROM (tblfriendships INNER JOIN " & _
            "tblposts ON tblfriendships.Friend_ID = tblposts.User_ID) INNER JOIN tblusers ON tblfriendships.Friend_ID = tblusers.User_ID " & _
            "WHERE ((tblfriendships.User_ID)=" & Session("User_ID") & " AND tblfriendships.Status=1)) " & _
            "UNION(SELECT tblposts.User_ID, tblposts.Post_ID, tblposts.Post, tblposts.TimeStamp, " & _
            "tblusers.First_Name, tblusers.Surname, tblusers.Profile_Picture FROM " & _
            "(tblposts INNER JOIN tblusers ON tblposts.User_ID = tblusers.User_ID) WHERE tblposts.User_ID = " & Session("User_ID") & ") " & _
            "ORDER BY tblposts.TimeStamp DESC;"
        ads_comments.SelectCommand = "SELECT tblcomments.*, tblusers.First_Name, tblusers.Surname, tblusers.Profile_Picture FROM (tblusers INNER JOIN (tblposts INNER JOIN tblcomments ON tblposts.Post_ID = tblcomments.Post_ID) ON tblusers.User_ID = tblcomments.User_ID) WHERE tblcomments.Post_ID=@Post_ID;"
        ads_comments.InsertCommand = "INSERT INTO [tblcomments] ([Post_ID], [User_ID], [Comment]) VALUES (13, " & Session("User_ID") & ", 'Something');"
    End Sub
    Protected Sub lv_recentmessages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lv_recentmessages.ItemDataBound
        Dim lv_comments As ListView = DirectCast(lv_recentmessages.FindControl("lv_comments"), ListView)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            lv_recentmessages.Items.Clear()
            Dim pid As String = DirectCast(e.Item.FindControl("hdn_postid"), HiddenField).Value
            If Not String.IsNullOrEmpty(pid) Then
                Post_ID = Convert.ToInt32(pid)
            End If
            Dim lvcomments As ListView = CType(e.Item.FindControl("lv_comments"), ListView)
            If (Not (lvcomments) Is Nothing) Then
                lvcomments.DataBind()
            End If
        End If
    End Sub

    Protected Sub ads_comments_Selecting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles ads_comments.Selecting
        e.Command.Parameters("Post_ID").Value = Post_ID
    End Sub

    Protected Sub btn_addcomment_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tb_addcomment As TextBox = DirectCast(lv_recentmessages.FindControl("tb_addcomment"), TextBox)

        Dim postcommentSQL As String
        postcommentSQL = "INSERT INTO [tblcomments] ([Post_ID], [User_ID], [Comment]) VALUES (@Post_ID, " & Session("User_ID") & ", '" & tb_addcomment.Text & "');"
        Dim PostCommentDataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/database.mdb"))
        Dim PostCommentCommand As New OleDbCommand(postcommentSQL, PostCommentDataConn)
        PostCommentDataConn.Open()
        PostCommentCommand.ExecuteNonQuery()
        PostCommentDataConn.Close()
        Response.Redirect(Request.RawUrl)
    End Sub

End Class



------------------

<%@ Page Language="VB" MasterPageFile="~/master_normal.master" AutoEventWireup="false" CodeFile="home.aspx.vb" Inherits="index" title="Visage // Home" %>

<asp:Content ID="Content2" ContentPlaceHolderID="cph_left" Runat="Server">
 <h1>Welcome <%=Session("First_Name")%> <%=Session("Surname")%></h1>
<p><strong>Post Message | Post Image</strong></p>
<asp:Panel runat="server" DefaultButton="btn_postmessage" >
<p><asp:TextBox runat="server" ID="tb_postmessage" Text="Enter your message" CssClass="d" /></p>
<p><asp:Button runat="server" id="btn_postmessage" Text="Post Message" /></p>
</asp:Panel>


    <div id="recentposts">
        <asp:ListView ID="lv_recentmessages" runat="server" DataSourceID="ads_recentmessages" DataKeyNames="Post_ID" >
            <ItemTemplate>
                    <li>
                        <asp:HiddenField ID="hdn_postid" runat="server" Value='<%#Eval("Post_ID") %>'/>
                        <p class="rp-pp"><asp:HyperLink ID="lnk_friendpic" runat="server" NavigateUrl='<%# "profile.aspx?UserID="& Eval("User_ID") %>'>
                        <asp:Image ID="img_profilepicture" runat="server" Width="50px" ImageUrl='<%# "userimages/pp/"& Eval("Profile_Picture") %>' /></asp:HyperLink></p>
                        <p><strong><asp:Literal ID="p_firstname" runat="server" Text='<%# Eval("First_Name") & " " %>' />
                        <asp:Literal ID="p_surname" runat="server" Text='<%# Eval("Surname") %>' /></strong> wrote on
                        <asp:Literal ID="p_timestamp" runat="server" Text='<%# Eval("TimeStamp") %>' /><br />
                        "<asp:Literal ID="p_post" runat="server" Text='<%# Eval("Post") %>' />"</p>
                                        
                <asp:ListView ID="lv_comments" runat="server" DataSourceID="ads_comments" DataKeyNames="Post_ID">
                    <ItemTemplate>
                            <li>
                                <p class="rp-pp"><asp:HyperLink ID="lnk_friendpic" runat="server" NavigateUrl='<%# "profile.aspx?UserID="& Eval("User_ID") %>'>
                                <asp:Image ID="img_profilepicture" runat="server" Width="25px" ImageUrl='<%# "userimages/pp/"& Eval("Profile_Picture") %>' /></asp:HyperLink></p>
                                <p><strong><asp:Literal ID="p_comfirstname" runat="server" Text='<%# Eval("First_Name") & " " %>' />
                                <asp:Literal ID="lit_comsurname" runat="server" Text='<%# Eval("Surname") %>' /></strong> wrote on
                                <asp:Literal ID="p_timestamp" runat="server" Text='<%# Eval("Timestamp") %>' /><br />
                                "<asp:Literal ID="p_comment" runat="server" Text='<%# Eval("Comment") %>' />"</p>
                            </li>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <div id="comments">
                            <p><strong>Recent Comments</strong></p>
                            <ul ID="itemPlaceholderContainer" runat="server">
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </ul>
                            </div>
                    </LayoutTemplate>
                </asp:ListView>
                
                        <p class="rp-pc"><asp:TextBox ID="tb_addcomment" runat="server" CssClass="d" Text="Enter your comment"
                        onfocus="if(this.value.toLowerCase() == 'enter your comment') this.value = ''; this.className='search a';" 
                        onblur="if(this.value.toLowerCase() == '') this.value = 'Enter your comment'; this.className='search d';" />
                        <asp:Button CommandName="Insert" ID="btn_addcomment" runat="server" Text="Add Comment" 
                            CssClass="ac" OnClick="btn_addcomment_OnClick"/>
                        </p>
                    </li>
            </ItemTemplate>

            <LayoutTemplate>
            <h2>Recent Postings</h2>
                <ul ID="itemPlaceholderContainer" runat="server" border="0" style="">
                    <tr ID="itemPlaceholder" runat="server">
                    </tr>
                </ul>
            </LayoutTemplate>
        </asp:ListView>
    </div>
        
    <p><asp:AccessDataSource ID="ads_recentmessages" runat="server" DataFile="~/App_Data/database.mdb" /></p>
    
    <p><asp:AccessDataSource ID="ads_comments" runat="server" DataFile="~/App_Data/database.mdb">
        <SelectParameters>
            <asp:ControlParameter Name="Post_ID" ControlID="lv_recentmessages" PropertyName="SelectedValue" Type="Int32" DefaultValue="1" />
        </SelectParameters>
        <InsertParameters>
            <asp:ControlParameter Name="Post_ID" ControlID="lv_recentmessages" PropertyName="SelectedValue" Type="Int32" DefaultValue="1" />
        </InsertParameters>
    </asp:AccessDataSource></p>
</asp:Content>

Open in new window

matthallettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
In order to use FindControl correctly, we need to understand the control hierarchy.  In order to find control effectively, you have a few choices:  a) use FindControl on the naming container, or b) use a recursive search (which is not very efficient).
0
guru_samiCommented:
One way is here:
Modify your insert button like:
<asp:Button CommandName="MyInsert" ID="btn_addcomment" runat="server" Text="Add Comment"
                             CommandArgument='<%# #Eval("Post_ID") %>'/>
Points to note:
1:  CommandName="MyInsert"
2: CommandArgument='<%# #Eval("Post_ID") %>'
3: remove onclick

    Protected Sub lv_recentmessages_ItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles  lv_recentmessages_ItemCommand
        If (e.CommandName = "MyInsert") Then
            Dim postid As Integer = Convert.ToInt32(e.CommandArgument)
            Dim tbcomment As TextBox = CType(e.Item.FindControl("tb_addcomment"),TextBox)
            Dim comment As String = tbcomment.Text
            'You have postid and comment text so do your sql insert here
        End If
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
matthallettAuthor Commented:
Perfect, worked straight away.
0
matthallettAuthor Commented:
Awesome. Thanks Sami, that worked perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.