Refreshing XML data when returning from (Adding Data) in Form2 back to Display Form1

mond007

Active member
Joined
Apr 26, 2010
Messages
37
Location
near Solihull, Birmingham UK
Programming Experience
10+
Hi
I have a simple database of 150 questions whereby Form1 is an Enquiry Screen where a QuestionNo is entered and the answer and its attributes are returned from an XML File for Display.

The XML File layout is as follows :
QuestionNo (e.g. 2.7)
Question Text (What should you do when some one faints).
AnswerSection (Health & Emergency)
AnswerSectionColor (Red) ('Colour Coded' as mentioned above)
AnswerImage (Image of a Man being tended to). JPG, BMP, etc.(importable).
AnswerHyperlink (www.n.h.s.recovery-situation-blah-blah.com

A second Form2 is used to maintain the 150 QuestionAnswerData.xml backend file.

The problem is that when I add or modify data in Form2 and "Save" the data in maintenance Form2, the changes are NOT displayed or reflected in Form1 the display.

I know the changes have been saved because if I totally come out of the entire application and re-launch then the newly added data is present. What I need to happen is when data is added I need a mechanism or code that will re-read the XML file data when return from Form2 to Form1.

Form1
Form1.jpg

Form2
Form2.jpg

VB.NET:
Imports System.Text.RegularExpressions
Public Class Form1
    Dim QuestionAnswerData As New DataSet
    Dim bsQuestions As New BindingSource
    Public Property DetectUrls As Boolean
    Public Property SelectionIndent As Integer
    Public Property SelectionHangingIndent As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '--------------------------------------------------------------------------
        QuestionAnswerData = New DataSet
        QuestionAnswerData.ReadXml(GlobalVariables.RootPath.ToString & GlobalVariables.RootXMLFileName.ToString, XmlReadMode.ReadSchema)

        With QuestionAnswerData.Tables(0).Columns(0)
            .AutoIncrement = True
            .AutoIncrementStep = 1
            If QuestionAnswerData.Tables(0).Rows.Count > 0 Then
                .AutoIncrementSeed = QuestionAnswerData.Tables(0).Rows.Cast(Of DataRow).Max(Function(x) CInt(x(0))) + 1
            Else
                .AutoIncrementSeed = 1
            End If
        End With

        With bsQuestions
            .DataSource = QuestionAnswerData
            .DataMember = "Questions"
        End With
        Me.txtb_search_question_no.Focus()
    End Sub
    Private Sub txtb_search_question_no_TextChanged(sender As Object, e As EventArgs) Handles txtb_search_question_no.TextChanged
        Dim myColor As Color = Color.Green
        Dim iColor As Integer = myColor.ToArgb()
        Dim sColor As String = iColor.ToString
        Dim DataViewRecord As DataView
        'Search the Questions Table for the one that you want to find 
        Dim foundRow As DataRow = QuestionAnswerData.Tables("Questions").Select(String.Format("QuestionNo ='{0}'", txtb_search_question_no.Text)).FirstOrDefault

        DataViewRecord = New DataView(QuestionAnswerData.Tables(0))
        DataViewRecord.Sort = "QuestionNo"
        Dim index As Integer = DataViewRecord.Find(Me.txtb_search_question_no.Text)

        If index = -1 Then
            'MsgBox("Question Not Found!")
            Me.RichTextBox1.Text = " QUESTION NOT FOUND "
            PictureBox2.Image = Nothing
        Else
            Me.RichTextBox1.Text = ""
            Me.txtb_hyperlink.Text = ""
            Me.txtb_section.Text = DataViewRecord(index)("AnswerSection").ToString()
            '------------------------- Load Answer Image -------------------------
            Dim fs As System.IO.FileStream
            fs = New System.IO.FileStream((GlobalVariables.RootImagesPath & Replace(Me.txtb_search_question_no.Text.ToString(), ".", "_") & ".png"), IO.FileMode.Open, IO.FileAccess.Read)
            PictureBox2.Image = System.Drawing.Image.FromStream(fs)
            '------------------------- Load Answer RichTextFile -------------------------
            If DataViewRecord(index)("AnswerHyperlink").ToString() <> "" Then
                Me.txtb_hyperlink.Text = DataViewRecord(index)("AnswerHyperlink").ToString()
            End If
            '------------------------- Load Answer RichTextFile -------------------------
            If My.Computer.FileSystem.FileExists(GlobalVariables.RootRtfPath & Replace(DataViewRecord(index)("QuestionNo").ToString(), ".", "_") & ".rtf") Then
                Me.RichTextBox1.LoadFile(GlobalVariables.RootRtfPath & Replace(DataViewRecord(index)("QuestionNo").ToString(), ".", "_") & ".rtf", RichTextBoxStreamType.RichText)
            End If
        End If
    End Sub

    Private Sub BtnMaintainData_Click(sender As Object, e As EventArgs) Handles BtnMaintainData.Click
        Form2.Show()
    End Sub
End Class
Public Class GlobalVariables
    Public Shared driver_installed As Boolean
    Public Shared RootPath As String = "C:\QuestionAnswer\"
    Public Shared RootImagesPath As String = "C:\QuestionAnswer\Images\"
    Public Shared RootRtfPath As String = "C:\QuestionAnswer\RichTextFiles\"
    Public Shared RootXMLFileName As String = "QuestionAnswerData.xml"
    Public Shared GlobalQuestionNo As String
End Class

Form2 - The Maintenance of Data

VB.NET:
Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.IO
Public Class Form2
    Dim QuestionAnswerData As New DataSet
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '--------------------------------------------------------------------------
        Dim bsQuestions As New BindingSource

        QuestionAnswerData.ReadXml(GlobalVariables.RootPath.ToString & GlobalVariables.RootXMLFileName.ToString, XmlReadMode.ReadSchema)

        With QuestionAnswerData.Tables(0).Columns(0)
            .AutoIncrement = True
            .AutoIncrementStep = 1
            If QuestionAnswerData.Tables(0).Rows.Count > 0 Then
                .AutoIncrementSeed = QuestionAnswerData.Tables(0).Rows.Cast(Of DataRow).Max(Function(x) CInt(x(0))) + 1
            Else
                .AutoIncrementSeed = 1
            End If
        End With

        With bsQuestions
            .DataSource = QuestionAnswerData
            .DataMember = "Questions"
        End With

        Me.BindingSource1.DataSource = QuestionAnswerData
        Me.BindingSource1.DataMember = "Questions"
        Me.DataGridView1.DataSource = Me.BindingSource1
        With DataGridView1
            .AllowUserToAddRows = False
            .AllowUserToResizeColumns = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False
            .Columns(0).Visible = False
            .RowHeadersVisible = False
            .Columns(1).HeaderText = "Question No"
            .Columns(1).Width = 60
            .Columns(2).HeaderText = "Description"
            .Columns(2).Width = 210
            .Columns(3).HeaderText = "Section"
            .Columns(3).Width = 145
            .Columns(4).HeaderText = "Section Colour"
            .Columns(4).Width = 90
            .Columns(5).HeaderText = "Image"
            .Columns(5).Width = 90
            .Columns(6).HeaderText = "Hyperlink"
            .Columns(6).Width = 329
        End With
        DataGridView1.DefaultCellStyle.Font = New Font("Trebuchet MS", 8)
        '------------------------------------------------------
        Dim bc As New DataGridViewButtonColumn
        bc.Tag = False
        bc.Text = "Delete"
        bc.Name = "Delete"
        bc.Width = 19
        DataGridView1.Columns.Add(bc)
        ActiveControl = DataGridView1
        DataGridView1.ReadOnly = False   ' Disable entire DataGridView to Read Only then set all the columns in your code as readonly.
        DataGridView1.Columns("QuestionNo").ReadOnly = True                 ' Disable changing of Main Question ID to prevent mismatch problems.

    End Sub
    Private Sub btnSaveData_Click(sender As Object, e As EventArgs) Handles btnSaveData.Click
        QuestionAnswerData.WriteXml(GlobalVariables.RootPath.ToString & GlobalVariables.RootXMLFileName.ToString, XmlWriteMode.WriteSchema)
        MsgBox("Question and Answers Information Saved", vbInformation)
    End Sub
End Class

It is well to say this is an extremely cut down skeleton version of a much bigger Application which took several months and I am positively hoping I will not have to abandon this at the 11th hour so to speak.

I believe this issue not insurmountable for someone who has superior knowledge of this type of Application. It is only a question of resetting or reloading the data once back from the second Form2.

Any help would be greatly appreciated as I believe there are many experts out there with far more experience than a novice like myself.

Thanks in Advance.
ps I have tried bindingSource1.ResetBindings(False) to no avail.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,463
Location
Norway
Programming Experience
10+
It is only a question of resetting or reloading the data once back from the second Form2.
That is correct, as with current code you have load the data again from xml file when that file is changed, because you're using different datasets in those forms. There are ways to achieve that, but since both forms are using same data it would be better that you passed QuestionAnswerData dataset from form1 to form2 and updated that, those changes would then be shown in form1 also.
 

mond007

Active member
Joined
Apr 26, 2010
Messages
37
Location
near Solihull, Birmingham UK
Programming Experience
10+
Indeed, I am glad someone sees the simplicity of this but my problem is that I am not how to pass the data from one form to the other. I am afterall a novice. :-s
I will research how to achieve this as I am confident its not that hard.

If you could provide a pointer then this would be great but I will have a go. Thanks
 

mond007

Active member
Joined
Apr 26, 2010
Messages
37
Location
near Solihull, Birmingham UK
Programming Experience
10+
I managed to find the solution in the end.

VB.NET:
Private Sub BtnMaintainData_Click(sender As Object, e As EventArgs) Handles BtnMaintainData.Click
    Form2.ShowDialog()
    UpdateView()
End Sub

Private Sub UpdateView()
    'code to update data here
End Sub

the long version is :

VB.NET:
Private Sub BtnMaintainData_Click(sender As Object, e As EventArgs) Handles BtnMaintainData.Click
    Form2.ShowDialog()
    UpdateView()
End Sub

Private Sub UpdateView()
'code to update data here
    QuestionAnswerData = New DataSet
    QuestionAnswerData.ReadXml(GlobalVariables.RootPath.ToString & GlobalVariables.RootXMLFileName.ToString, XmlReadMode.ReadSchema)

With QuestionAnswerData.Tables(0).Columns(0)
    .AutoIncrement = True
    .AutoIncrementStep = 1
    If QuestionAnswerData.Tables(0).Rows.Count > 0 Then
        .AutoIncrementSeed = QuestionAnswerData.Tables(0).Rows.Cast(Of DataRow).Max(Function(x)CInt(x(0))) + 1
     Else
        .AutoIncrementSeed = 1
     End If
End With

With bsQuestions
    .DataSource = QuestionAnswerData
    .DataMember = "Questions"
End With
End Sub

Hope this helps anyone.

Thanks for all you help.
 
Top Bottom