Solved

Gridview displaying repeating same row even though data is different

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

687 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