Link to home
Start Free TrialLog in
Avatar of psychokraft
psychokraft

asked on

VB Application only reads every other field in XML file.

Hello, see the attached files. I aim to move XML data into a multiline textbox on form1 from the xml file. Everything works exactly as it should, except for the fact that the textbox only receives every other value. The code for this operation begins on line 208 of form1.vb, sub procedure btnSaveXMLData_Click. Thanks in advance for the help! SimpleCalculator.exe XMLforSimpleCalc
'Name           Simple Caculator
'Purpose        To create calculation and save them in a 10 object array
'Programer      Robert Kraft on 3/13/2011

Option Strict On
Option Explicit On
Option Infer Off

Imports System.Data.SqlClient
Imports System.Xml



Public Class frmCalc

    Private mathoperation As MathOperations
    Private Const path As String = "C:\Users\Robert\Desktop\AMU\ENTD461\Week7\XMLforSimpleCalc"
    Dim resultsList As New List(Of String)(10)
    Dim dblFirstNum As Double
    Dim dblSecondNum As Double
    Dim dblResult As Double
    Dim strOperation As String
    Dim strResults As String
    Dim newQuid As String

   

    Private Sub radAddition_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radAddition.CheckedChanged
        txtOperation.Text = "+"
        strOperation = txtOperation.Text
    End Sub

    Private Sub radSubtraction_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radSubtraction.CheckedChanged
        txtOperation.Text = "-"
        strOperation = txtOperation.Text
    End Sub

    Private Sub radMultiplication_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radMultiplication.CheckedChanged
        txtOperation.Text = "*"
        strOperation = txtOperation.Text
    End Sub

    Private Sub radDivision_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radDivision.CheckedChanged
        txtOperation.Text = "/"
        strOperation = txtOperation.Text
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Double.TryParse(txtFirstNumber.Text, dblFirstNum)
        Double.TryParse(txtSecondNumber.Text, dblSecondNum)

        If radAddition.Checked Then
            mathoperation = New Addition(CDbl(txtFirstNumber.Text), CDbl(txtSecondNumber.Text), txtOperation.Text)
        ElseIf radSubtraction.Checked Then
            mathoperation = New Subtraction(CDbl(txtFirstNumber.Text), CDbl(txtSecondNumber.Text), txtOperation.Text)
        ElseIf radMultiplication.Checked Then
            mathoperation = New Multiplication(CDbl(txtFirstNumber.Text), CDbl(txtSecondNumber.Text), txtOperation.Text)
        ElseIf radDivision.Checked Then
            If dblSecondNum <> 0 Then
                mathoperation = New Division(CDbl(txtFirstNumber.Text), CDbl(txtSecondNumber.Text), txtOperation.Text)
            Else : dblSecondNum = 0
                MessageBox.Show("Cannot Divide by Zero", _
                                "Zero Division", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtSecondNumber.Focus()
                txtSecondNumber.SelectAll()
                txtResult.Text = "Nothing"
            End If
        End If

        If txtResult.Text = "Nothing" Then
            txtResult.Text = ""
        Else
            txtResult.Text = mathoperation.GetFinalValue
        End If
       
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        resultsList.Add(txtResult.Text)

        txtFinalResults.Text = Nothing
        txtFirstNumber.Text = Nothing
        txtSecondNumber.Text = Nothing
        txtResult.Text = Nothing
        txtFirstNumber.Focus()

    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim final As String = ""
        For Each d As String In resultsList
            final &= d & vbCrLf
        Next
        txtFinalResults.Text = final

       
    End Sub

    Private Sub frmCalc_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'TODO: This line of code loads data into the 'DataForSimpleCalculatorDataSet.Updated' table. You can move, or remove it, as needed.
        Me.UpdatedTableAdapter.Fill(Me.DataForSimpleCalculatorDataSet.Updated)
        'TODO: This line of code loads data into the 'DataForSimpleCalculatorDataSet.Equations' table. You can move, or remove it, as needed.
        Me.EquationsTableAdapter.Fill(Me.DataForSimpleCalculatorDataSet.Equations)
        radAddition.Checked = True

        txtFinalResults.Text = Nothing
        txtFirstNumber.Text = Nothing
        txtSecondNumber.Text = Nothing
        txtResult.Text = Nothing
        txtQuid.Text = Nothing
        txtFirstNumber.Focus()
        mtxtDate.Text = CStr(Date.Now)
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtFinalResults.Text = ""
        resultsList.Clear()
    End Sub

    Private Sub txtFirstNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFirstNumber.KeyPress, txtSecondNumber.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
            AndAlso e.KeyChar <> ControlChars.Back _
            AndAlso e.KeyChar <> "-" Then
            e.Handled = True
        End If
    End Sub

   

  
    Private Sub btnSaveToDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveToDatabase.Click


        Try

            Dim equationrow As DataForSimpleCalculatorDataSet.EquationsRow
            equationrow = Me.DataForSimpleCalculatorDataSet.Equations.NewEquationsRow
            equationrow.Equations = txtFinalResults.Text
            equationrow.ID = CInt(CInt(txtQuid.Text) & vbCrLf)
            Me.DataForSimpleCalculatorDataSet.Equations.AddEquationsRow(equationrow)
            EquationsTableAdapter.Update(Me.DataForSimpleCalculatorDataSet.Equations)


            Dim dateRow As DataForSimpleCalculatorDataSet.UpdatedRow
            dateRow = Me.DataForSimpleCalculatorDataSet.Updated.NewUpdatedRow
            dateRow.WhenUpdated = CDate(mtxtDate.Text)
            dateRow.ID = CInt(txtQuid.Text)
            Me.DataForSimpleCalculatorDataSet.Updated.AddUpdatedRow(dateRow)
            UpdatedTableAdapter.Update(Me.DataForSimpleCalculatorDataSet.Updated)
        Catch ex As SqlException
            MessageBox.Show("SQL Server error # " & ex.Number & ": " & ex.Message, ex.GetType.ToString)
        Catch ex As Exception
            MessageBox.Show("The Key entered already exists in the database. Please coose another key", "Key Error")


        End Try

        txtFinalResults.Text = Nothing
        txtFirstNumber.Text = Nothing
        txtSecondNumber.Text = Nothing
        txtResult.Text = Nothing
        txtQuid.Text = Nothing
        resultsList.Clear()
        txtFirstNumber.Focus()

    End Sub

   
    Private Sub txtQuid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuid.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
           AndAlso e.KeyChar <> ControlChars.Back _
           Then
            e.Handled = True
        End If
    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Dim F2 As New Form2
        F2.show()

    End Sub


    Private Sub btnSaveXMLData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXMLData.Click
        Dim settings As New XmlWriterSettings
        settings.Indent = True
        settings.IndentChars = ("    ")

        Dim xmlOut As XmlWriter = XmlWriter.Create(path, settings)

        xmlOut.WriteStartDocument()
        xmlOut.WriteStartElement("EquationsList")

        xmlOut.WriteElementString("ID", txtQuid.Text)

        For i As Integer = 0 To resultsList.Count - 1
           
            xmlOut.WriteElementString("Equations", resultsList(i))
        Next

        xmlOut.WriteElementString("WhenUpdated", mtxtDate.Text)

        xmlOut.WriteEndElement()
        xmlOut.Close()
    End Sub

    Private Sub btnOpenXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenXML.Click

        Dim settings As New XmlReaderSettings
        settings.IgnoreComments = True
        settings.IgnoreWhitespace = True

        Dim xmlin As XmlReader = XmlReader.Create(path, settings)

        If xmlin.ReadToDescendant("EquationsList") Then

            xmlin.ReadStartElement("EquationsList")
            txtQuid.Text = xmlin.ReadElementContentAsString

            Do
                Dim i As String = xmlin.ReadElementContentAsString

                resultsList.Add(i)
                
            Loop While xmlin.ReadToNextSibling("Equations")

            Dim final As String = ""
            For Each d As String In resultsList
                final &= d & vbCrLf
            Next
            txtFinalResults.Text = final

        End If
        xmlin.Close()
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial