Solved

Gridview displaying repeating same row even though data is different

Posted on 2011-02-25
4
407 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Initial get page response times and IIS8 15 51
Javascript to set controls visibility 5 36
VB.NET 1 23
Following an example - Why do I need to click Save button twice? 3 25
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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