xml for editing in gridview and xls output

Last post 07-02-2009 4:00 PM by ejowens. 2 replies.

Sort Posts:

  • xml for editing in gridview and xls output

    06-29-2009, 2:14 PM
    • Member
      10 point Member
    • ejowens
    • Member since 12-14-2007, 11:54 AM
    • St Louis Metro area
    • Posts 97

    Using vwd2005 in asp.net 2, I am trying to create an xml document in memory so that it can be edited in a gridview and in turn be output to excel.  As a file, I was able to load into gridview and edit, but in production, cannot use a file because the application is load balanced.  In tryin to work with a memory stream object, i was getting:

    System.InvalidCastException was unhandled by user code
      Message="Xml type 'xdt:untypedAtomic' does not support a conversion from Clr type 'XmlDeclaration' to Clr type 'String'."
      Source="System.Xml"

    So I tried to create the document type, and am now getting this error:

    System.InvalidOperationException was unhandled by user code
      Message="Cannot insert the node in the specified location."

     

    Any help would be appreciated.

        Protected Sub MakeOL_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim Adapter As New VPNMasterTableAdapters.SRSrvcsEquipDefaultsTableAdapter
            Dim Table As VPNMaster.SRSrvcsEquipDefaultsDataTable
            Dim Row As VPNMaster.SRSrvcsEquipDefaultsRow
    
            'Get the Selected Order List
            Table = Adapter.GetSRSrvcsEquipDefaultsByListName(ddlOrderList.SelectedValue.ToString())
    
            If Table.Rows.Count > 0 Then
    
                Dim xOL As New XmlDocument()
                Dim xType As XmlDocumentType
                Dim xdecl As XmlDeclaration
                Dim root As XmlNode = xOL.DocumentElement
                Dim element As XmlNode
                'get last integer for use in CSV to XML
                Dim iPN As Integer
    
                'Build XML Doc
                xType = xOL.CreateDocumentType("OrderList", Nothing, Nothing, Nothing)
                xOL.AppendChild(xType)
                xdecl = xOL.CreateXmlDeclaration("1.0", Nothing, Nothing)
                xOL.AppendChild(xdecl)
                root = xOL.CreateElement("OrderList")
                xOL.AppendChild(root)
    
                'Create XML from the Selected Order List
                For Each Row In Table.Rows()
                    element = xOL.CreateElement("item")
                    'Stock
                    element.AppendChild(xOL.CreateNode("element", "Stock", ""))
                    element("Stock").InnerText = Row.Stock
                    'Activity
                    element.AppendChild(xOL.CreateNode("element", "Activity", ""))
                    element("Activity").InnerText = Row.Activity
                    'EquipOwned
                    element.AppendChild(xOL.CreateNode("element", "EquipOwned", ""))
                    element("EquipOwned").InnerText = Row.EquipOwned
                    'Manufacturer
                    element.AppendChild(xOL.CreateNode("element", "Manufacturer", ""))
                    element("Manufacturer").InnerText = Row.Manufacturer
                    'EHSN
                    element.AppendChild(xOL.CreateNode("element", "EHSN", ""))
                    element("EHSN").InnerText = Row.EHSN
                    'Qty
                    element.AppendChild(xOL.CreateNode("element", "Qty", ""))
                    element("Qty").InnerText = Row.Qty
                    'Part Number
                    element.AppendChild(xOL.CreateNode("element", "PartNumber", ""))
                    element("PartNumber").InnerText = Row.PartNumber
                    'Description
                    element.AppendChild(xOL.CreateNode("element", "Description", ""))
                    element("Description").InnerText = Row.Description
                    'ID
                    element.AppendChild(xOL.CreateNode("element", "ID", ""))
                    element("ID").InnerText = Row.ID
                    iPN = Row.ID
    
                    root.AppendChild(element)
                Next
    
                'Convert CSV File to XML
                If System.IO.File.Exists(GetCSV.Value) Then
                    Dim CSVReader As System.IO.StreamReader = New System.IO.StreamReader(GetCSV.Value)
                    Dim ColName(), ColValue(), line, header As String
                    Dim PN, Status, i As Integer
    
                    Do
                        i = i + 1
                        line = CSVReader.ReadLine()
                        element = xOL.CreateElement("item")
    
                        If i = 1 Then
                            header = Replace(line, " ", "")
                            ColName = header.Split(",")
                            PN = Array.IndexOf(ColName, "PartNumber")
                            Status = Array.IndexOf(ColName, "Status")
                        Else
                            If Not line Is Nothing Then
                                ColValue = line.Split(",")
                                If ColValue(Status) = "Installed" Then
    
                                    'make view that sets up partnumbers with manufacturers
                                    'use PN to look up necessary elements and add to XML
                                    Dim csvAdapter As New VPNMasterViewsTableAdapters.EquipmentDataTableAdapter
                                    Dim csvTable As VPNMasterViews.EquipmentDataDataTable
                                    Dim csvRow As VPNMasterViews.EquipmentDataRow
                                    Dim strManufacturer, strDescription As String
    
                                    'Some items in csv may not have been entered in the equipment table and we
                                    'need to accommodate for values that can't be looked up
                                    csvTable = csvAdapter.GetData(ColValue(PN))
                                    If csvTable.Rows.Count > 0 Then
                                        csvRow = csvTable.Rows(0)
                                        strManufacturer = csvRow.Manufacturer
                                        strDescription = csvRow.Description
                                    Else
                                        strManufacturer = "unknown"
                                        strDescription = "unknown"
                                    End If
    
                                    element.AppendChild(xOL.CreateNode("element", "Stock", ""))
                                    element("Stock").InnerText = ""
                                    'Activity
                                    element.AppendChild(xOL.CreateNode("element", "Activity", ""))
                                    element("Activity").InnerText = "R"
                                    'EquipOwned
                                    element.AppendChild(xOL.CreateNode("element", "EquipOwned", ""))
                                    element("EquipOwned").InnerText = "C"
                                    'Manufacturer
                                    element.AppendChild(xOL.CreateNode("element", "Manufacturer", ""))
                                    element("Manufacturer").InnerText = strManufacturer
                                    'EHSN
                                    element.AppendChild(xOL.CreateNode("element", "EHSN", ""))
                                    element("EHSN").InnerText = i.ToString() + ".0"
                                    'Qty
                                    element.AppendChild(xOL.CreateNode("element", "Qty", ""))
                                    element("Qty").InnerText = "1"
                                    'Part Number
                                    element.AppendChild(xOL.CreateNode("element", "PartNumber", ""))
                                    element("PartNumber").InnerText = ColValue(PN)
                                    'Description
                                    element.AppendChild(xOL.CreateNode("element", "Description", ""))
                                    element("Description").InnerText = strDescription
                                    'ID
                                    element.AppendChild(xOL.CreateNode("element", "ID", ""))
                                    element("ID").InnerText = IIf(iPN = 0, i, iPN + i)
    
                                    root.AppendChild(element)
    
                                End If
                            End If
                        End If
                    Loop Until line Is Nothing
                    CSVReader.Close()
                End If
    
                Dim xStream As New MemoryStream
                Dim xWriter As New XmlTextWriter(xStream, System.Text.Encoding.UTF8)
    
                xWriter.WriteStartDocument(True)
                xWriter.WriteValue(xOL)
                fName = xStream
                'Save and Apply XML Doc
                Dim xDS As New DataSet
                xDS.ReadXml(xStream)
                xDS.ReadXml(xStream)
    
                gvEditOL.Visible = True
                MakeOLLink.Visible = False
                ExportLink.Visible = True
    
            End If
    
            lblMakeOL.Visible = False
            GetCSV.Visible = False
            lblOLSelect.Visible = False
            ddlOrderList.Visible = False
    
        End Sub
    


    Regards,

    Eric

  • Re: xml for editing in gridview and xls output

    06-29-2009, 4:33 PM
    • Member
      10 point Member
    • ejowens
    • Member since 12-14-2007, 11:54 AM
    • St Louis Metro area
    • Posts 97

    Found i was placing the document type into the wrong location...ooops  :D

     

    Now the situation has changed.  I modified the code as follows (starts at line 138 from previous code entry):

                Dim xStream As MemoryStream = New MemoryStream()
                xOL.Save(xStream)
                xStream.Flush()
                xStream.Position = 0
                xName = xStream
    
                'Apply XML Doc as dataset
                Dim xDS As New DataSet
                xDS.ReadXml(xStream)
    
    At xDS.ReadXML(xStream) i get an error saying the root element is missing.  According to many google searches and some items in the forums,
    the position of the stream is set to 0 before being read into the dataset.  What am I missing?
     
    Thanks,
     
    Eric
     

     

     

  • Re: xml for editing in gridview and xls output

    07-02-2009, 4:00 PM
    Answer
    • Member
      10 point Member
    • ejowens
    • Member since 12-14-2007, 11:54 AM
    • St Louis Metro area
    • Posts 97

    Well it seems that restarting web dev 2005 solved that problem!
     

Page 1 of 1 (3 items)