locked
Displaying an image from an Access database using RowDataBound in the Gridview RRS feed

  • Question

  • User-359420042 posted

    I have a gridview which holds a load of user comments, their user names, and hopefully their user avatar. I have a database which has a table called 'userprofiles' which has a field inside of it called 'AvatarURL'. I want to retrieve the URL of the image for each user who has previously left a comment. The unique identifier in the database is 'TravellerName' for each user.

    I have managed to get this far (although I am not so sure that I am anywhere near getting this right so maybe not!):

    Client Side:

    <%-- Comments Box --%>
         <asp:GridView ID="GridView2" OnRowDataBound="GridView2_RowDataBound" runat="server" AutoGenerateColumns="False" 
            DataSourceID="CommentsDataSource" Height="167px" Width="325px">
            <Columns>
              <asp:TemplateField HeaderText="Comments">
                <ItemTemplate>
                <div style="background-color:Silver">
                <div class="avatar-frame">
                <asp:Image ID="ProfilePic" runat="server" OnRowDataBound="GridView2_RowDataBound"/>
                </div>
                <h1><%# Eval("TagLine")%></h1>
                <h2><%# Eval("IfNonMemberUserName")%></h2>
                <p><%# Eval("CommentBody")%></p>
                </div>
                </ItemTemplate>
                <AlternatingItemTemplate>
                <div style="background-color:White">
                <div class="avatar-frame">
                <asp:Image ID="ProfilePic" runat="server" OnRowDataBound="GridView2_RowDataBound" />
                </div>
                <h1><%# Eval("TagLine")%></h1>
                <h2><%# Eval("IfNonMemberUserName")%></h2>
                <p><%# Eval("CommentBody")%></p>
                </div>
                </AlternatingItemTemplate>
                </asp:TemplateField>
            </Columns>
            
        </asp:GridView>
    
        <asp:SqlDataSource ID="CommentsDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:BookMeetConnString %>" 
            ProviderName="<%$ ConnectionStrings:BookMeetConnString.ProviderName %>" 
            SelectCommand="SELECT [IfNonMemberUserName], [UserAvatar], [TagLine], [CommentBody] FROM [comments] WHERE ([BookID] = ?)">
            <SelectParameters>
                <asp:QueryStringParameter Name="?" QueryStringField="ID" />
            </SelectParameters>
        </asp:SqlDataSource>
        <%-- End of Comments Box --%>

    Code Behind:

        Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
            Dim sql = "SELECT AvatarURL FROM userprofiles WHERE TravellerName=@f1"
            Dim cmd = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            Dim ProfilePic
            If e.Row.RowType = DataControlRowType.DataRow Then
                ProfilePic = e.Row.FindControl("ProfilePic")
                ProfilePic = sql
            End If
        End Sub
    

    Although thinking about it, this code (if it worked) would only display the avatar of the user currently logged in. How can I successfully display avatars for all users who have left a comment?


    Sunday, March 31, 2013 10:05 AM

Answers

  • User1508394307 posted

    1) Get rid of GridView2_RowDataBound() method
    2) Modify SelectCommand to select AvatarUrl, e.g. 

    SelectCommand="SELECT IfNonMemberUserName, UserAvatar, TagLine, 
    CommentBody, AvatarUrl FROM comments WHERE BookID=?">

    or make required join if AvatarUrl is in another table

    SelectCommand="SELECT IfNonMemberUserName, UserAvatar, TagLine, 
    CommentBody, AvatarUrl FROM comments, userprofiles WHERE BookID=? AND userprofiles.userid=comments.userid">

    3) Modify ProfilePic as 

    <asp:Image ID="ProfilePic" runat="server" ImageUrl='<%# Eval("AvatarUrl") %>' />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 31, 2013 4:34 PM

All replies

  • User1508394307 posted

    1) Get rid of GridView2_RowDataBound() method
    2) Modify SelectCommand to select AvatarUrl, e.g. 

    SelectCommand="SELECT IfNonMemberUserName, UserAvatar, TagLine, 
    CommentBody, AvatarUrl FROM comments WHERE BookID=?">

    or make required join if AvatarUrl is in another table

    SelectCommand="SELECT IfNonMemberUserName, UserAvatar, TagLine, 
    CommentBody, AvatarUrl FROM comments, userprofiles WHERE BookID=? AND userprofiles.userid=comments.userid">

    3) Modify ProfilePic as 

    <asp:Image ID="ProfilePic" runat="server" ImageUrl='<%# Eval("AvatarUrl") %>' />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 31, 2013 4:34 PM
  • User-359420042 posted

    Thank you very much, this is perfect!

    Monday, April 1, 2013 7:26 AM