Database choice?

  • Thread starter Thread starter Orion Tatsihama
  • Start date Start date
If youd like to just read in a text file, heres a class I put together that will do it. It just reads the file in and stores the result in a string variable, accessed through the filedata property. If theres an error, the errormessage property will be non-blank. Not much in the way of comments, but theres not much code either.

By the way, Im also new at this. Then again, isnt everybody?

Code:
Public Class OpenForInput
    Private strFile As String
    Private intHandle As Integer
    Private strData As String
    Private strError As String

    Public Property FileName() As String
        Get
            Return strFile
        End Get

        Set(ByVal Value As String)
            strFile = Value
            If Dir(strFile) = "" Then
                strError = "File " & strFile & " not found."
                Exit Property
            End If

            Dim lngLength As Long
            lngLength = FileLen(strFile)

            strError = ""
            strData = ""
            intHandle = FreeFile()
            Try
                FileOpen(intHandle, strFile, OpenMode.Input, OpenAccess.Read, OpenShare.Default, )
                strData = InputString(intHandle, lngLength)

            Catch aru As System.ArgumentException
                strError = aru.Message

            Catch ios As System.IO.IOException
                strError = ios.Message

            Catch exe As Exception
                strError = exe.Message

            Finally
                FileClose(intHandle)
                If strError > "" Then    had an error
                    If strData = "" Then     means couldnt read file
                        strError = "Couldnt read file. " & Chr(13) & strError
                    Else
                        strError = "Couldnt open file. " & Chr(13) & strError
                    End If
                End If


            End Try

        End Set
    End Property

    Public ReadOnly Property ErrorMessage() As String
        Get
            Return strError
        End Get
    End Property

    Public ReadOnly Property FileData() As String
        Get
            Return strData
        End Get
    End Property

End Class
 
Last edited by a moderator:
I went this gjnaves code and DVlog (which I replaced ds1 with, just the name) and ViewDVlog (which I replaced datagrid1 with) arent valid...they have little blue underlines on them.
 
Heres the code (forgot to post it).

Code:
Public Class Form
    Inherits System.Windows.Forms.Form

    Public DVlog As New DataSet()
    Dim xmlDoc As New Xml.XmlDataDocument()

[ Windows Form Designer generated code ]

   DVlog.ReadXml(fileOpen) 

    Private Sub saveXMLFile()
        Dim sDialog As New SaveFileDialog()
        sDialog.FileName = ("DVconfig") & ".xml"
        sDialog.Filter = "XML files (*.xml)|*.xml"
        If sDialog.ShowDialog = DialogResult.OK Then
            Dim wstream As New _
            System.IO.StreamWriter(sDialog.FileName)
            DVlog.WriteXml(wstream, XmlWriteMode.WriteSchema)
            wstream.Close()
        End If
    End Sub

    ViewDVlog.datasource = ds1 
    ViewDVlog.readonly = true 

    Dim fsReadXml As New System.IO.FileStream("DVconfig.xml", System.IO.FileMode.Open)
     Create an XmlTextReader to read the file. 
    Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
     Read the XML document into the DataSet. 
    DVblog.ReadXml(myXmlReader) 
     Close the XmlTextReader 
    myXmlReader.Close() 
End Class
I edited out the Windows Form Designer generated code (as you can see) because it made this post too long.
 
Last edited by a moderator:
You have a syntax error

Orion,

You have the correct code but you inserted it wrong.
First thing, you mispelled your variable "DVlog" . You typed:
DVblog.ReadXml(myXmlReader)

Secondly, and most importantly, you didnt have your code within a subroutine (Sub).

You have the first part of your code inside a subroutine (SaveXML)
Code:
Private Sub saveXMLFile()
        DVlog.ReadXml(fileOpen)
        Dim sDialog As New SaveFileDialog()
        sDialog.FileName = ("DVconfig") & ".xml"
        sDialog.Filter = "XML files (*.xml)|*.xml"
        If sDialog.ShowDialog = DialogResult.OK Then
            Dim wstream As New _
            System.IO.StreamWriter(sDialog.FileName)
            DVlog.WriteXml(wstream, XmlWriteMode.WriteSchema)
            wstream.Close()
        End If
    End Sub

but the rest is not:

Code:
    ViewDVlog.datasource = ds1 
    ViewDVlog.readonly = true 

    Dim fsReadXml As New System.IO.FileStream("DVconfig.xml", System.IO.FileMode.Open)
     Create an XmlTextReader to read the file. 
    Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
     Read the XML document into the DataSet. 
    DVblog.ReadXml(myXmlReader) 
     Close the XmlTextReader 
    myXmlReader.Close() 
End Class

Do you understand what I mean? You have to have the above code inside a Sub... end Sub. Visual Basic will not recognize your variables otherwise.

What i do is I have a button that when pressed will load the xml into the datagrid when i press it. SO it should look something like this:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim fsReadXml As New System.IO.FileStream("DVconfig.xml", System.IO.FileMode.Open)
     Create an XmlTextReader to read the file. 
    Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
     Read the XML document into the DataSet. 
    DVlog.ReadXml(myXmlReader) 
     Close the XmlTextReader 
    myXmlReader.Close() 

End Sub
In other words... create a button on your form, and then double click it. The code will come up. Then place the code inside there.
You can also place the code in the Load Sub procedure if you want it to do it automatically when the program starts. Whatever, but the main thing is that it HAS to be within a sub procedure

THe code you use to set the datagrid to read-only also has to be inside a sub procedure. So maybe put it in the Sub Load procedure or set it at design time.

If you get this correct the code will work. If for some reason it is not finding your xml file, then you can try and use a fileOpenDialog to show it where the file is. But you can cross that bridge when you come to it.

Let me know how it goes!

jim
 
Last edited by a moderator:
oops.. i forgot to mention what to do with this important line of code:
Code:
ViewDVlog.datasource = ds1

you should put that with the rest of the code as shown below (or even better put it in your form load procedure):

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

[U]ViewDVlog.datasource = ds1[/U]
Dim fsReadXml As New System.IO.FileStream("DVconfig.xml", System.IO.FileMode.Open) 
 Create an XmlTextReader to read the file. 
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml) 
 Read the XML document into the DataSet. 
DVlog.ReadXml(myXmlReader) 
 Close the XmlTextReader 
myXmlReader.Close() 

End Sub
 
Last edited by a moderator:
Thanks because your first directions confused the crap out of me. Let me do this and Ill update ya.
 
Problem, in the:
Code:
 Private Sub saveXMLFile()
        DVlog.ReadXml(FileOpen)
        Dim sDialog As New SaveFileDialog()
        sDialog.FileName = ("DVconfig") & ".xml"
        sDialog.Filter = "XML files (*.xml)|*.xml"
        If sDialog.ShowDialog = DialogResult.OK Then
            Dim wstream As New _
            System.IO.StreamWriter(sDialog.FileName)
            DVlog.WriteXml(wstream, XmlWriteMode.WriteSchema)
            wstream.Close()
        End If
    End Sub
The DVlog.ReadXml(FileOpen) has an error at FileOpen. It says: Argument not specified for parameter...blah blah blah.
 
Last edited by a moderator:
fileOpen = ?

Right... because FileOpen needs to be specified. It is just a variable to hold the file location (i.e. "mySchema.xsd")

Second of all it doesnt belong in your Sub SaveXMLFile procedure:

it belongs in your load procedure like follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

DVlog.ReadXml(FileOpen)


but dont actually type "FileOpen".. put your schema (.xsd) location there: for example: DVlog.ReadXML("xmlSchema.xsd")
This loads the xml schema that you created into your dataset.


Im sorry that i didnt catch (and repeated) your placement mistake on the last message.

jim
 
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DVlog.ReadXml(xmlSchema.xsd)
    End Sub
Has an error at xmlSchema (which is what my xml schema is called). It says something about: System.Data.XMLSchema is not accessible in this context because it is Private
 
because you have to put xmlSchema.xsd in parantheses. Like this:

DVlog.ReadXML("xmlSchema.xsd")
 
Code:
Public Class Form
    Inherits System.Windows.Forms.Form

    Public DVlog As New DataSet()
    Dim xmlDoc As New Xml.XmlDataDocument()



#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        This call is required by the Windows Form Designer.
        InitializeComponent()

        Add any initialization after the InitializeComponent() call

    End Sub

    Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    NOTE: The following procedure is required by the Windows Form Designer
    It can be modified using the Windows Form Designer.  
    Do not modify it using the code editor.
    Friend WithEvents Cancel As System.Windows.Forms.Button
    Friend WithEvents Submit As System.Windows.Forms.Button
    Friend WithEvents DVDProps As System.Windows.Forms.GroupBox
    Friend WithEvents Comments As System.Windows.Forms.TextBox
    Friend WithEvents Year As System.Windows.Forms.TextBox
    Friend WithEvents SE As System.Windows.Forms.ComboBox
    Friend WithEvents Disks As System.Windows.Forms.TextBox
    Friend WithEvents Length As System.Windows.Forms.TextBox
    Friend WithEvents Rating As System.Windows.Forms.ComboBox
    Friend WithEvents Title As System.Windows.Forms.TextBox
    Friend WithEvents Genre As System.Windows.Forms.TextBox
    Friend WithEvents CommentsLabel As System.Windows.Forms.Label
    Friend WithEvents YearLabel As System.Windows.Forms.Label
    Friend WithEvents SELabel As System.Windows.Forms.Label
    Friend WithEvents DisksLabel As System.Windows.Forms.Label
    Friend WithEvents LengthLabel As System.Windows.Forms.Label
    Friend WithEvents RatingLabel As System.Windows.Forms.Label
    Friend WithEvents TitleLabel As System.Windows.Forms.Label
    Friend WithEvents GenreLabel As System.Windows.Forms.Label
    Friend WithEvents ViewDVlog As System.Windows.Forms.DataGrid
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Cancel = New System.Windows.Forms.Button()
        Me.Submit = New System.Windows.Forms.Button()
        Me.DVDProps = New System.Windows.Forms.GroupBox()
        Me.Comments = New System.Windows.Forms.TextBox()
        Me.Year = New System.Windows.Forms.TextBox()
        Me.SE = New System.Windows.Forms.ComboBox()
        Me.Disks = New System.Windows.Forms.TextBox()
        Me.Length = New System.Windows.Forms.TextBox()
        Me.Rating = New System.Windows.Forms.ComboBox()
        Me.Title = New System.Windows.Forms.TextBox()
        Me.Genre = New System.Windows.Forms.TextBox()
        Me.CommentsLabel = New System.Windows.Forms.Label()
        Me.YearLabel = New System.Windows.Forms.Label()
        Me.SELabel = New System.Windows.Forms.Label()
        Me.DisksLabel = New System.Windows.Forms.Label()
        Me.LengthLabel = New System.Windows.Forms.Label()
        Me.RatingLabel = New System.Windows.Forms.Label()
        Me.TitleLabel = New System.Windows.Forms.Label()
        Me.GenreLabel = New System.Windows.Forms.Label()
        Me.ViewDVlog = New System.Windows.Forms.DataGrid()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.DVDProps.SuspendLayout()
        CType(Me.ViewDVlog, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()

        [I took out a bunch of Form Designer code to shorten message]

        
        RatingLabel
        
        Me.RatingLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.RatingLabel.Location = New System.Drawing.Point(192, 72)
        Me.RatingLabel.Name = "RatingLabel"
        Me.RatingLabel.Size = New System.Drawing.Size(40, 16)
        Me.RatingLabel.TabIndex = 3
        Me.RatingLabel.Text = "Rating"
        
        TitleLabel
        
        Me.TitleLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.TitleLabel.Location = New System.Drawing.Point(8, 72)
        Me.TitleLabel.Name = "TitleLabel"
        Me.TitleLabel.Size = New System.Drawing.Size(32, 16)
        Me.TitleLabel.TabIndex = 2
        Me.TitleLabel.Text = "Title"
        
        GenreLabel
        
        Me.GenreLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.GenreLabel.Location = New System.Drawing.Point(8, 32)
        Me.GenreLabel.Name = "GenreLabel"
        Me.GenreLabel.Size = New System.Drawing.Size(40, 16)
        Me.GenreLabel.TabIndex = 1
        Me.GenreLabel.Text = "Genre"
        
        ViewDVlog
        
        Me.ViewDVlog.DataMember = ""
        Me.ViewDVlog.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.ViewDVlog.Location = New System.Drawing.Point(360, 8)
        Me.ViewDVlog.Name = "ViewDVlog"
        Me.ViewDVlog.Size = New System.Drawing.Size(368, 416)
        Me.ViewDVlog.TabIndex = 10
        
        Button1
        
        Me.Button1.Location = New System.Drawing.Point(168, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(72, 32)
        Me.Button1.TabIndex = 11
        Me.Button1.Text = "Refresh"
        
        GroupBox1
        
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Cancel, Me.Submit, Me.Button1})
        Me.GroupBox1.Location = New System.Drawing.Point(56, 368)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(248, 56)
        Me.GroupBox1.TabIndex = 12
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Controls"
        
        Form
        
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(736, 430)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox1, Me.ViewDVlog, Me.DVDProps})
        Me.Name = "Form"
        Me.Text = "DVLog"
        Me.DVDProps.ResumeLayout(False)
        CType(Me.ViewDVlog, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

        
        Dataset
        
        ViewDVlog.DataSource = DVlog
        ViewDVlog.ReadOnly = True
    End Sub
At the ViewDVlog.DataSource = DVlog it says: The variable DVlog is either undeclared or was never assigned.
 
argh!
Ok... you put it in the wrong place again.
Dont even mess with the code you showed above.
Dont even expand the "#Region " Windows Form Designer generated code ". You dont need it and wont even touch it.

Do this... go to your form where you can see the actual way it looks... double click somewhere on the form (not on a button or datagrid.. but just on the blank form)...
the code window will come up and you will be inside of " Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load"

Put, your code THERE.

ViewDVlog.dataSource = DVlog
ViewDVlog.ReadOnly = true

make sure its BETWEEN sub form1_load (etc...)
and
end sub


Also make sure you delete it out of the wrong place you put it.
jim
 
Now when I try to debug/test the program, I get this:
An unhandled exception of type System.IO.FileNotFoundException occurred in system.xml.dll

Additional information: Could not find file "H:\Documents and Settings\Orion\My Documents\Visual Studio Projects\DVlog\bin\xmlSchema.xsd".

At this line: DVlog.ReadXml("xmlSchema.xds")
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DVlog.ReadXml("xmlSchema.xsd")
        ViewDVlog.DataSource = DVlog
        ViewDVlog.ReadOnly = True
    End Sub
And xmlSchema.xsd is the name of my schema.
 
i wasnt mad... if i was mad I wouldnt respond anymore :)
Now, read your error and see if you can figure out what the problem is:
Could not find file "H:\Documents and Settings\Orion\My Documents\Visual Studio Projects\DVlog\bin\xmlSchema.xsd".

So obviously its not able to find the file "xmlSchema.xsd"... did you create a xsd file the way i explained it?
Secondly, if you did and its not in the directory that the program is looking for it in, then either, 1) move it to that directory (short term answer) or 2) create an openFIleDialog and navigate to it.

I suggest that for the sake of initially getting the program to work that you just move the file to the directory that its looking for it in. ("H:\Documents and Settings\Orion\My Documents\Visual Studio Projects\DVlog\bin\") - And then AFTER you have had the program work succesfully worry about making a more reasonable way of finding files.

let me know,

jim
 
I dont even know where the file is...I created it just like you told me. And when this program is released, wouldnt having a Save and Open file dialogue enable multiple databases?
 
Back
Top