Solved

Gridview displaying repeating same row even though data is different

Posted on 2011-02-25
4
409 Views
Last Modified: 2012-05-11
I am trying display the result I get from an xml document in a gridview. When I read the xml document I use a linq query to get the specific results.  I loop through each element on the xml document and add the required results to a datarow.

The datarow is added to a datatable, then a dataset and finally bound to a gridview. Everything seems to function correctly until I acutally display the gridview. The number of rows are correct but it always displays the results of my final row in every row.

It be great if someone can point me in the right direction. Thanks
 Gridview
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

Open in new window

Imports System.Xml
Imports System.IO
Imports System
Imports System.Data.DataView


Public Class _Default
    Inherits System.Web.UI.Page
    Dim row As DataRow
    Dim ds As New DataSet
    Dim dt As New DataTable



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.DataSource = dt
        dt.TableName = "FTPLog"
        dt.Columns.Add("Name")
        dt.Columns.Add("Time")
        dt.Columns.Add("Status")

        ds.Tables.Add(dt)
        Dim xmlDate As String = Format(Now, "dd/MM/yyyy")
        Dim xmlTime As String = Format(Now, "hh:mm:ss")


        Dim xmlDoc As XDocument = XDocument.Load("http://repecomm/xml/ftptransfers_02_2011.xml")
        Dim root As XElement = xmlDoc.Root

        Dim StockAdjustmentVendaResult = From q In xmlDoc.Descendants("ftp")
        Where (q.Element("id").Value = "Stock Adjustment Venda") _
        And q.Element("date").Value = xmlDate
        Select q

        Dim mike As String

        For Each x In StockAdjustmentVendaResult

            row = dt.NewRow

            For Each child As XElement In StockAdjustmentVendaResult.Descendants()

                mike = child.Name.ToString

                Select Case mike

                    Case "info"

                        If child.Value.Contains("226 Transfer complete") Then
                            row.Item("status") = ("The job has completed successfully")

                        Else
                            row.Item("status") = ("The job has failed")

                        End If

                    Case "id"

                        row.Item("name") = "Stock Adjustment Venda"


                    Case "start"
                        row.Item("time") = child.Value.ToString
                    Case Else
                        ' do nothing

                End Select

                ' dt.Rows.Add((String.Format("{0} {1}", child.Name.ToString(), child.Value)))


            Next

            dt.Rows.Add(row)
            GridView1.DataBind()

        Next


        
    End Sub
End Class

Open in new window

0
Comment
Question by:Michael Roodt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 34980777
can you please provide a sample of your xml?
0
 
LVL 2

Author Comment

by:Michael Roodt
ID: 34981097
I've attached an example  of the xml.
Thanks
example.xml
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 34981666
you seem to be making an iteration / loop too many.
Check out the results from this

        For Each x In StockAdjustmentVendaResult

            row = dt.NewRow()
            row.Item("status") = If(x.Element("info").Value.ToString.Contains("226 Transfer complete"), "The job has completed successfully", "The job has failed")
            row.Item("name") = "Stock Adjustment Venda"
            row.Item("time") = x.Element("start").Value.ToString

            dt.Rows.Add(row)
        Next

Open in new window


I am not sure whether there are some checks / data validations missing from my code, however, I get 4 distinct rows from my code

XMLClass.png
0
 
LVL 2

Author Comment

by:Michael Roodt
ID: 34981804
Brilliant! Thank you very much. I think I was confusing myself trying different methods. Appreciate you looking at this for me.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question