We help IT Professionals succeed at work.

trying to fill in a nested datalist with 1 sqldatareader

attipa
attipa asked
on
380 Views
Last Modified: 2011-10-03
i have the following code behind:

--------------------------------
Private Sub bloglist_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles bloglist.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim blogreader As SqlDataReader
            Dim commentreader As SqlDataReader
            Dim blogID As String
            Dim blogs As New Blog
            Dim blogimagelist As System.Web.UI.WebControls.DataList = DirectCast(e.Item.FindControl("blogimagelist"), System.Web.UI.WebControls.DataList)

            blogID = DataBinder.Eval(e.Item.DataItem, "BlogID").ToString()

            blogreader = blogs.GetBlogbyID(blogID)
           
            If blogreader.Read() Then
                BindCategories(categorylist, blogID)
                categorylist.Visible = True
                FiledUnderLabel.Visible = True
                FiledUnderExtraSpace.Visible = True
                datepostedforblog.Text = Format(CDate(blogreader("DateAdded")), "MMMM dd, yyyy")

                If Not blogreader("Picture1") = "0" Then
                    Dim Picture1URL As String
                    If blogreader("Picture1") = blogreader("ImageID") Then
                        Picture1URL = blogreader("URL")
                        mainblogimage.ImageUrl = "../admin-createblogmainimage.aspx?filename=../BlogImages/" + Picture1URL + "&width=250"
                        mainblogimage.Visible = True
                    End If
                Else
                    mainblogimage.Visible = False
                End If

                If Not blogreader("Picture2") = "0" Then
                    blogimagelist.Visible = True

                    blogimagelist.DataSource = blogreader("URL")
                    blogimagelist.DataKeyField = "ImageID"
                    blogimagelist.DataBind()
                End If

            Else
                categorylist.Visible = False
                FiledUnderLabel.Visible = False
                FiledUnderExtraSpace.Visible = False
            End If


            commentreader = comment.GetComments(blogID)
            If commentreader.Read() Then
                commentcounter += 1
            End If
            CommentsCountNumber.Text = "VENTING ROOM (" + commentcounter.ToString() + ")"
            CommentsCountNumber.NavigateUrl = "../blog-detail.aspx?BlogID=" + blogID

            blogreader.Close()

        End If
    End Sub
---------------------------------------

the following is the stored procedure:
---------------------------------------------
CREATE PROCEDURE GetBlogbyID
(@BlogID int)
AS

Select Blog.BlogID, Headline, [Text], Blog.DateAdded, Picture1, Picture2, Picture3, Picture4, Picture5, Picture6, Picture7, Picture8, Category1, Category2, Category3, Category4, Category5, Category6, Category7, Category8, Category.[Name], Category.CategoryID, CommentID, Comment.DateAdded, UserName, UserEmail, UserURL, [Image].ImageID, URL
From Blog Inner JOIN Category
ON Category.CategoryID = Blog.Category1 OR Category.CategoryID = Blog.Category2 OR Category.CategoryID = Blog.Category3 OR Category.CategoryID = Blog.Category4 OR Category.CategoryID = Blog.Category5 OR Category.CategoryID = Blog.Category6 OR Category.CategoryID = Blog.Category7 OR Category.CategoryID = Blog.Category8
Left Join Comment
On Comment.BlogID = Blog.BlogID
Left Join [Image]
On [Image].ImageID = Blog.Picture1 OR [Image].ImageID = Blog.Picture2 OR [Image].ImageID = Blog.Picture3 OR [Image].ImageID = Blog.Picture4 OR [Image].ImageID = Blog.Picture5 OR [Image].ImageID = Blog.Picture6 OR [Image].ImageID = Blog.Picture7 OR [Image].ImageID = Blog.Picture8
Where Blog.BlogID = @BlogID AND Category.CategoryID <> 7 AND [Image].ImageID <> 0
ORDER BY
     Case
          WHEN [Image].ImageID = Blog.Picture1 THEN 1
          WHEN [Image].ImageID = Blog.Picture2 THEN 2
          WHEN [Image].ImageID = Blog.Picture3 THEN 3
          WHEN [Image].ImageID = Blog.Picture4 THEN 4
          WHEN [Image].ImageID = Blog.Picture5 THEN 5
          WHEN [Image].ImageID = Blog.Picture6 THEN 6
          WHEN [Image].ImageID = Blog.Picture7 THEN 7
          WHEN [Image].ImageID = Blog.Picture8 THEN 8
          ELSE 10
     END

Return
GO
------------------------------------------

when the aspx page is loaded, I get the following error:

DataBinder.Eval: 'System.Char' does not contain a property with the name ImageID.

WHY?

the sql stored procedure returns all the information perfectly
Comment
Watch Question

Top Expert 2006

Commented:
Hi attipa,

What is the underlying datasource that is being bound to the bloglist DataList, is it a DataSet or DataReader?

Let's say it's a DataSet.. try this:

> blogID = DataBinder.Eval(e.Item.DataItem, "BlogID").ToString()

Replace that line with:

Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)
blogID = CStr(row("BlogID"))

Let's see what that produces...

-- Jason

Author

Commented:
its a dataset...everything works perfect until it gets to the part of blogimagelist.datakeyfield
Top Expert 2006

Commented:
I believe your problem is not in your code-behind but in your ASPX page.. can you show us that code?  Your error is attempting to extract the ImageID column out of a Character (System.Char) object... which is very odd.  But it appears to be happening elsewhere than what you've shown.

-- Jason

Author

Commented:
<asp:datalist id="bloglist" Runat="server" HorizontalAlign="Left" RepeatDirection="Vertical" RepeatColumns="1">
                        <ItemTemplate>
                        <div style="border-style: double; border-color: white; padding-top: 2px; padding-bottom: 2px; padding-left: 2px; padding-right: 2px">
                        <table width="450" cellpadding="8" cellspacing="0" bgcolor="#FFFFFF" border="0">
                              <tr>
                                    <td align="center" valign="center">
                              <table width="100%" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" border="0">
                                    <tr>
                                          <td align="left" valign="top">
                                                <font class="blog-list-headline">
                                                      <a class="blog-list-headline" href="../blog-detail.aspx?BlogID=<%# DataBinder.Eval(Container.DataItem, "BlogID") %>"><%# DataBinder.Eval(Container.DataItem, "Headline") %></a><br>
                                                </font>
                                          </td>
                                    </tr>
                                    <tr>
                                          <td align="left" valign="top">
                                                <div style="float: left; padding-left: 10px; height: 20px">
                                                <font class="blog-list-posteddate">
                                                      Posted: <asp:Label ID="datepostedforblog" Runat="server" CssClass="blog-list-posteddate"></asp:Label>
                                                </font>
                                                </div>
                                          </td>
                                    </tr>
                                    <tr>
                                          <td id="categorycell" align="left" valign="top">
                                                <div style="float: left; width: 90px; padding-left: 15px">
                                                      <asp:Label ID="FiledUnderLabel" Runat="server" CssClass="blog-list-category">Filed under:</asp:Label>
                                                </div>
                                                <div>
                                                      <asp:datalist id="categorylist" RepeatColumns="3" RepeatDirection="Horizontal" HorizontalAlign="Left"
                                                            Runat="server" Visible="False">
                                                            <ItemTemplate>
                                                                  <table border="0" cellpadding="0" cellspacing="0">
                                                                        <tr>
                                                                              <td align="left" valign="top">
                                                                                    <asp:HyperLink ID="categorylink" Runat="server" CssClass="blog-list-category">
                                                                                          <a class="blog-list-category" href="../category-detail.aspx?categoryID=<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>"><%# DataBinder.Eval(Container.DataItem, "Name") %></a>
                                                                                    </asp:HyperLink>
                                                                              </td>
                                                                        </tr>
                                                                  </table>
                                                            </ItemTemplate>
                                                            <SeparatorTemplate>
                                                                  <table border="0" cellpadding="0" cellspacing="0">
                                                                        <tr>
                                                                              <td align="left" valign="top">
                                                                                    <font class="blog-list-category">&nbsp;&nbsp;|&nbsp;&nbsp;</font>
                                                                              </td>
                                                                        </tr>
                                                                  </table>
                                                            </SeparatorTemplate>
                                                      </asp:datalist>
                                                </div>
                                          </td>
                                    </tr>
                                    <tr>
                                          <td align="center" valign="top">
                                                <asp:Label ID="FiledUnderExtraSpace" Runat="server" CssClass="blog-list-text"><br></asp:Label>
                                                <asp:Image ID="mainblogimage" Visible="False" Runat="server" />
                                          </td>
                                    </tr>
                                    <tr>
                                          <td align="left" valign="top">
                                                <font class="blog-list-text">
                                                      <%# DataBinder.Eval(Container.DataItem, "Text") %>
                                                      <br>
                                                </font>
                                          </td>
                                    </tr>
                                    <tr>
                                          <td align="left" valign="top" ID="picturescell" runat="server">
                                                <asp:datalist id="blogimagelist" RepeatColumns="4" RepeatDirection="Horizontal" HorizontalAlign="Left"
                                                      Runat="server" Visible="False">
                                                      <ItemTemplate>
                                                            <table width="100" height="100" border="0" cellpadding="0" cellspacing="0">
                                                                  <tr>
                                                                        <td align="center" valign="middle">
                                                                              <a onclick="MyWindow=window.open('../image-detail.aspx?ImageID=<%# DataBinder.Eval(Container.DataItem, "ImageID") %>','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=700,left=10,top=10'); return false;" href="#"><img src='../admin-createthumbnail.aspx?filename=../BlogImages/<%# DataBinder.Eval(Container.DataItem, "URL") %>&width=100' border="0"></a>
                                                                        </td>
                                                                  </tr>
                                                            </table>
                                                      </ItemTemplate>
                                                      <SeparatorTemplate>
                                                            <table width="5" cellpadding="0" cellspacing="0" border="0">
                                                                  <tr>
                                                                        <td align="center" valign="middle">
                                                                              <font class="blog-list-headline">
                                                                                    <br>
                                                                                    <br>
                                                                              </font>
                                                                        </td>
                                                                  </tr>
                                                            </table>
                                                      </SeparatorTemplate>
                                                </asp:datalist>
                                          </td>
                                    </tr>
                                    <tr>
                                          <td align="right" valign="top">
                                                <font class="blog-list-comments-label">
                                                      <asp:hyperlink ID="CommentsCountNumber" CssClass="blog-list-comments-label" Runat="server"></asp:hyperlink>
                                                      <br>
                                                </font>
                                          </td>
                                    </tr>
                              </table>
                              </td>
                              </tr>
                        </table>
                              </div>
                        </ItemTemplate>
                        <SeparatorTemplate>
                        <div style="padding-top: 2px; padding-bottom: 2px; padding-left: 2px; padding-right: 2px">
                              <table width="450" cellpadding="0" cellspacing="0" border="0">
                                    <tr>
                                          <td align="center" valign="top">
                                                <font class="blog-list-headline">
                                                      <br>
                                                      <hr width="90%">
                                                      <br>
                                                </font>
                                          </td>
                                    </tr>
                              </table>
                        </div>
                        </SeparatorTemplate>                  
                  </asp:datalist>

Author

Commented:
scolja,

any suggestions?  please help me out on this..I am stuck and have no clue as to what to do.
Top Expert 2006

Commented:
attipa,

I'm not seeing it.. try commenting out the blogimagelist.datakeyfield line, which you say is the line causing the error and see what happens... let's start debugging this and see what comes of it.. you only have a few references to the ImageID... hopefully it can't be too difficult to find.  But the error you posted appears to be with the DataBinder.Eval(Container.DataItem, "ImageID") line... so maybe try removing that and seeing what happens...

<a onclick="MyWindow=window.open('../image-detail.aspx?ImageID=<%# DataBinder.Eval(Container.DataItem, "ImageID") %>','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=700,left=10,top=10'); return false;" href="#"><img src='../admin-createthumbnail.aspx?filename=../BlogImages/<%# DataBinder.Eval(Container.DataItem, "URL") %>&width=100' border="0"></a>

-- Jason

Author

Commented:
This is the part of the code behind
--------------------------
                If Not blogreader("Picture2") = "0" Then
                    blogimagelist.Visible = True

                    blogimagelist.DataSource = blogreader("URL")
                    blogimagelist.DataKeyField = "ImageID"
                    blogimagelist.DataBind()
                End If


This is the nested datalist:
-----------------------------------
                                        <asp:datalist id="blogimagelist" RepeatColumns="4" RepeatDirection="Horizontal" HorizontalAlign="Left"
                                             Runat="server" Visible="False">
                                             <ItemTemplate>
                                                  <table width="100" height="100" border="0" cellpadding="0" cellspacing="0">
                                                       <tr>
                                                            <td align="center" valign="middle">
                                                                 <a onclick="MyWindow=window.open('../image-detail.aspx?ImageID=<%# DataBinder.Eval(Container.DataItem, "ImageID") %>','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=700,left=10,top=10'); return false;" href="#"><img src='../admin-createthumbnail.aspx?filename=../BlogImages/<%# DataBinder.Eval(Container.DataItem, "URL") %>&width=100' border="0"></a>
                                                            </td>
                                                       </tr>
                                                  </table>
                                             </ItemTemplate>
                                             <SeparatorTemplate>
                                                  <table width="5" cellpadding="0" cellspacing="0" border="0">
                                                       <tr>
                                                            <td align="center" valign="middle">
                                                                 <font class="blog-list-headline">
                                                                      <br>
                                                                      <br>
                                                                 </font>
                                                            </td>
                                                       </tr>
                                                  </table>
                                             </SeparatorTemplate>
                                        </asp:datalist>
Top Expert 2006

Commented:
attipa,

When I said I'm not seeing it.. I mean I'm not seeing what the error is exactly (through my own troubleshooting eyes).  Therefore, I need your help in debugging it... try the stuff I mentioned in my last post and let's see where that gets us.

-- Jason

Author

Commented:
here is the error:

DataBinder.Eval: 'System.Char' does not contain a property with the name ImageID.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinder.Eval: 'System.Char' does not contain a property with the name ImageID.

Source Error:


Line 80:                                                                   <tr>
Line 81:                                                                         <td align="center" valign="middle">
Line 82:                                                                               <a onclick="MyWindow=window.open('../image-detail.aspx?ImageID=<%# DataBinder.Eval(Container.DataItem, "ImageID") %>','MyWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=700,left=10,top=10'); return false;" href="#"><img src='../admin-createthumbnail.aspx?filename=../BlogImages/<%# DataBinder.Eval(Container.DataItem, "URL") %>&width=100' border="0"></a>
Line 83:                                                                         </td>
Line 84:                                                                   </tr>
 
Top Expert 2006

Commented:
attipa,

Try swapping out line 82, with this:
<a onclick='<%# "MyWindow=window.open(""../image-detail.aspx?ImageID=" & DataBinder.Eval(Container.DataItem, "ImageID") & """,""MyWindow"",""toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=700,left=10,top=10""); return false;" %>'  href="#"><img src='../admin-createthumbnail.aspx?filename=../BlogImages/<%# DataBinder.Eval(Container.DataItem, "URL") %>&width=100' border="0"></a>


-- Jason

Author

Commented:
same error.
Top Expert 2006

Commented:
attipa,

And if you remove that whole line... does it work?

Just for fun... let's change that line to this:

<%# Container.DataItem.GetType.ToString() %>

That should tell us what type of object the Container.DataItem is... your error keeps telling us it's a System.Char... let's confirm.

-- Jason

Author

Commented:
it is System.Char
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
now we are in business....however, it skips the first record.  It is suppose to produce 3 records...but only produces 2.  And also, since there are 3 rows that are returned from the blogreader....it produces the parent datalist three times...how can we fix these last two problems?
Top Expert 2006

Commented:
attipa,

Can you show me a screenshot?

-- Jason

Author

Commented:
Instead of just producing:
----------------------------
1   2   3
----------------------------

It is producing:
----------------------------
2   3
2   3
2   3
----------------------------
Top Expert 2006

Commented:
That's for the Inner DataList?

Author

Commented:
both.

this is what I want happening
----------------------
1   2   3     is the inner datalist
it should only produce 1 time...the outer datalist
----------------------

this is what is happening
----------------------
2   3   inner datalist produces only 2 results instead of 3
2   3
2   3
outer datalist is producing three times
----------------------

Author

Commented:
i think what is happening is since there is 3 rows that the sqldatareader is producing

the first row is going to the outer datalist

the 2nd and 3rd rows are going to the inner datalist

how do I re-read and entire dataset produced by sqldatareader?
Top Expert 2006

Commented:
attipa,

I think you would have to go with something like a DataSet/DataTable in that situation....

-- Jason
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.