Solved

Gridview displaying repeating same row even though data is different

Posted on 2011-02-25
4
404 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
Comment Utility
can you please provide a sample of your xml?
0
 
LVL 2

Author Comment

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

Accepted Solution

by:
nepaluz earned 500 total points
Comment Utility
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
Comment Utility
Brilliant! Thank you very much. I think I was confusing myself trying different methods. Appreciate you looking at this for me.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now