Solved

Gridview displaying repeating same row even though data is different

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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:…
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 …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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