Page 1 of 2 12 LastLast
Results 1 to 40 of 62

Thread: [RESOLVED] Simple Database [text file] for Pocket PC

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Resolved [RESOLVED] Simple Database [text file] for Pocket PC

    Now for reasons I will not go into here I need to learn the methods required in VB.net mobile apps to read and write to a simple text file.

    I have spent two days trying to get my head around it but to date with no success.

    I need to know how it’s done, my requirements are simple I have about twenty variables and need to load there values and save there values when I need to as the user clicks various buttons.

    So at the program load I may need the values of the previously saved variables say var2, var6, var8 and var9. Then if the user presses a button I may need to save the value of var19 say.

    Now on the face of it this would appear simple but every time I try VB.Net tells me I have done it wrong and that method is not support etc….

    Can anyone help or point me to a simple example (and I do mean simple).

    Thanks ladies and gentlemen

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Simple Database for Pocket PC

    So, what you really need is how to do file I/O on .NET Mobile.
    1) Change the title of this thread to reflect this
    2) Have a mod move it to the Mobile App section.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database for Pocket PC

    1. I don’t know what an I/O is
    2. I don’t know how to get it moved
    3. Many thanks for your help!!

    Tell me were all of your 6,800 posts as helpful?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Simple Database [text file] for Pocket PC

    Moved to .Net Mobile Dev. forum, and thread title changed

    1. I don’t know what an I/O is
    Input/Output - basically reading & writing a file.

    2. I don’t know how to get it moved
    the best way is to send a PM to a moderator/admin (you were lucky I spotted this thread). If you look at the bottom of our main page, moderators/admins are shown in orange or red.

    Tell me were all of your 6,800 posts as helpful?
    There are times when the best answer you can give is advice on where/how to get the answer.. this is one of those times.

  5. #5
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Simple Database [text file] for Pocket PC

    Hi,
    so what have you tried?

    Is each variable in a different record or is it a CSV file or similar?

    If you change a value, do you need to write it back?

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Peter Vick,

    CSV File?

    I have a number of variable values that are created by my app, I just want to be able to read and write to a text file. My problem is I don’t know how to do it I was quite happy in VB6 using access with ADODC but now in .NET for mobile app’s all that easy linking and simple read write functionality has gone. I don’t have SQL Server and don’t know how to use it anyway.

    So I have come to the conclusion that a simple text file is the way to go as I don’t need a big data store or to query it. I just simply want to read and write values to a know location so I can then get them back, either after I close the program or change a setting when using it.

  7. #7
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Simple Database [text file] for Pocket PC

    Hi,
    csv is a comma separated variable file.
    If you look up 'streamreader' or 'streamwriter' in the help file, there are a couple of sections at the bottom of the page that will help...
    Other Resources
    File and Stream I/O
    How to: Read Text from a File
    How to: Write Text to a File
    Basic File I/O
    How to: Read and Write to a Newly Created Data File


    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Pete Vick,

    You say "other resources"

    File and Stream I/O
    How to: Read Text from a File
    How to: Write Text to a File
    Basic File I/O
    How to: Read and Write to a Newly Created Data File

    Were do I find these resources?

  9. #9
    Member
    Join Date
    Mar 2007
    Posts
    55

    Re: Simple Database [text file] for Pocket PC

    A quick google of "Read Text from a File VB.Net"

    http://www.freevbcode.com/ShowCode.asp?ID=4492

    came up alot of hits...
    gl

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Many thanks for all of the input to-date.

    Am I right in thinking that if I use this stream reader then each time I write to this file I must write the values of all of the variables I want to save and similarly when I read it I must read all of the values of all of the variables I save to it?

    If this is the case then how do I know which value is which?

    Or do I use a different text file for the value of each variable?
    Last edited by JohnSavage; May 3rd, 2007 at 08:23 AM. Reason: spelling & more info

  11. #11
    Member
    Join Date
    Mar 2007
    Posts
    55

    Re: Simple Database [text file] for Pocket PC

    I think you can easily write a program that just appends or changes the values in the textfile.
    No need for multiple textfiles.

  12. #12
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Simple Database [text file] for Pocket PC

    Hi,
    the 'other resources' are hot links in the help file at the bottom of the page.

    Read in your variables from your file as input and close it.
    When they change, open it as output, and write them all back, and close it again

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Pete

    There is no help file at the bottom of this page?

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple Database [text file] for Pocket PC

    From the sounds of your initial question, I think you might consider only reading from the textfile once (at program startup), and writing to the file once or more. Sure, you can read the whole thing in and out of a text file whenever you want, but it is generally a waste of time and resources, and has plenty of opportunity to fail.

    Read everything at startup.
    Store it all in an array or a class or structure.
    Deal only with this memory image for all work.
    Write the array out to a text file whenever you feel the need to save your work.

    Also, considering the number of variables you are talking about, and the size of the data, I would avoid CSV, and simply write one variable to each line. This makes reading and writing simpler.

    As for how to do it.....I have a class that you could use that would take care of the whole thing. Unfortunately, I won't be near my computers for ten days or so (I'm across the country dealing with a family emergency), so you can't have the class from me anytime soon....unless I already posted it here, which I think I have tried to do a few times. You might search on my posts from about a year back. Frankly, I think there was something funky with the code, and the class never posted successfully.
    My usual boring signature: Nothing

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Shaggy

    Your offer sounds good to me and I will have to wait for it, although I have to say I do not know what a class is!

    But in the meantime if you could just tell me how to write one value to one line and then how to get back any lines value then I would be away and coding.

    I have spent all day looking at the few books I have and have come to the conclusion that a binary file and read write would be great but again when I get to use microsofts help I cannot find an example that works on the mobile platform the example I have in my book is

    filenum=freefile

    open "Try.Dat" for Binary as Filenum

    But this does not work as it appears not to be supported, the blood is now flowing freely down the wall from were I have been banging my head

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple Database [text file] for Pocket PC

    The joy of a class is that once you have it working, you just create an instance of it and use the methods. Since this was one of the first classes I wrote in .NET, I can't even answer your question well. Whenever I want to deal with text files (and I do a fair amount), I just create an instance of the class, passing in the filename, then call ReadLine to read a line, and WriteLine to write a line. How does it work? Well......I knew when I wrote it, and if I saw it again, I could figure it out, but that was a couple years ago, and I have never dealt with it since. That's what classes are all about. Create something that works well, and you can drop it into whatever project you want, and forget about what is happening under the hood.

    All I can say at this time (until I get home) is that what petevick was trying to direct you towards is the way to go. He was suggesting the MSDN topics, which is what I used to create my file class.

    Other than that, I would suggest searching the .NET forum for "read text file" or streamreader. The topic is relatively common, and I have seen some pretty comprehensive code snippets.

    Still, I believe you should be thinking about this in terms of reading the whole file in right off, keeping the data in memory (an array, or individual variables), and writing all the stuff to the file as needed. I can tell you that you won't notice the time taken to write that many variables to a text file. The time is so insignificant that you shouldn't even worry about it.
    My usual boring signature: Nothing

  17. #17
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Simple Database [text file] for Pocket PC

    Hi John,
    are you sure your book is on VB.Net - the statement
    filenum=freefile
    looks more evb/VB6 to me. What version of Visual Studio are you running?

    To read from a file
    Code:
     Dim sr As StreamReader
            Dim strInput As String
    
            sr = New StreamReader("\My Documents\MyFile.txt")
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do While sr.Peek() >= 0
                strInput = sr.ReadLine()
    	    'strinput now contains a record from the file
    	    'deal with it how you need to here
            Loop 'Until strInput Is Nothing
            sr.Close()
    To write to a file
    Code:
     Dim sw As StreamWriter = File.CreateText("\My Documents\MyFile.txt")
    	sw.Write("This is a line")
    	sw.Close()
    As I said earlier, all this is in the help file.

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Pete & all the other kind enough to reply,

    Here is my test code to-date:

    Code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Dim Write As IO.StreamWriter
        Dim Read As IO.StreamReader
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Write = New IO.StreamWriter("\program files\textwriter\MyFile.txt")
            Write.WriteLine(TextBox1.Text)
            Write.WriteLine(TextBox2.Text)
            Write.Close()
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim String1 As String
    
            Read = New IO.StreamReader("\program files\textwriter\MyFile.txt")
    
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do While Read.Peek() >= 0
                String1 = Read.ReadLine()
                'strinput now contains a record from the file
                'deal with it how you need to here
            Loop 'Until strInput Is Nothing
            Read.Close()
    
            TextBox3.Text = String1
    
        End Sub
    
    End Class
    Now bear with me and I will explain my problem, this test app writes the default values of textbox1 and textbox2 which are “textbox1” and “textbox2” when I press button one and when I press button2 it writes the first line of the file to textbox3.

    When I examine the file it was two lines of text the first line is “textbox1” and the second line is “textbox2”. Now my question is how do I get these values back from the file selectively? That is if I want the value of line two to be assigned to textbox4 when I press the read button how is it done?

    Now as I hope you all understand that is and has always been my problem in setting out with this method if I save values to a text file I need to get them back and assign them back to the right variables or else the whole exercise is pointless.

    How do I get the value of line2 and assign it ? or line8 or line11 etc
    Last edited by JohnSavage; May 4th, 2007 at 07:15 AM.

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple Database [text file] for Pocket PC

    The simple answer is: don't even try.

    There is no good way to write to just a portion of a file. In general, a file is the start of the file and its length. If you wanted to adjust one element in a file, the file would have to track the location of every element, along with its size, and since bytes are stored sequentially, changing the length of any element would force a change in all the table members following that element. Basically, that isn't going to happen for you.

    Therefore, you have to think in terms of reading ALL the elements in, and writing ALL the elements each time. Consider having an array of strings: myStrings(5) as string

    Have one function that reads the file, and reads all the strings into the array in one shot. Then you can use the array in the fashion that you wanted to use the file originally. It will be faster, too, because arrays are in memory, and don't require any disc access (though, since this is a portable device, this is kind of irrelevant).

    You will also need a function that writes all the members of the array into the file, each one in sequence. Once the read and write functions are written, you call the read function at the start of the program, and call the write function whenever you feel you should 'save your work'. The write function could be called whenever you change an array element, or it could be called only when you quit the program. That is up to you, but there should never be a reason to read from the file more than once at the start of the program.
    My usual boring signature: Nothing

  20. #20
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Simple Database [text file] for Pocket PC

    Hi,
    I agree, or, use SqlServerCE - sledgehammer to crack a nut, but it will do exactly what you want, but easier

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Shaggy, Pete

    You seem to agree so who am I to question?

    Perhaps first I should explain I bit more, I started this thread with the statement “for reasons I will not explain” But I will now as you both have tried hard to help.

    I get by in VB6 using access and would have never asked any questions like this if I could use it with .NET mobile development, but no its gone so then it was sql compact database or nothing, so I though OK lets try it and try it I did for 4 days and eventually found out that I could not use it as I do not have sql (the big one) installed as a result of starting with express in 2003 and then not installing it when I upgraded to 2005 (and now I cannot find the CD), so that the history behind this sorry tale.

    Now back to the plot, I know because of your help that I can write and read from a text file, so how do I

    1. get the file into an array (load the data)

    2. get the data from the array into the file (write the data to file)

    3. reference the array when using the app to update the values


    and yes before you ask I have searched this forum for the answer but I can find no simple examples

  22. #22
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple Database [text file] for Pocket PC

    I wish I was back at my computer, I've got examples of it all.

    By the way, you do have SQLServerCE on the portable, since it gets installed whenever you try to do just about anything. However, if you intend to move it to a desktop system, then it won't be quite as easy. Frankly, based on the size of the data, I think the text file is a fine solution.


    If nobody answers this in the next week, I'll have an answer for you. Off the top of my head, I don't think I can remember anything about streamreaders, though.
    My usual boring signature: Nothing

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Shaggy

    Many thanks!

  24. #24
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple Database [text file] for Pocket PC

    Here's a thread on how I solved this type of problem

    http://www.vbforums.com/showthread.php?t=461690

    It uses an XML file for storing the data - which actually is quite nice, it open in IE instead of notepad - and it's the modern way to "store" a couple of dozen elements.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  25. #25
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Simple Database [text file] for Pocket PC

    Hi,
    good call - I originally ignored XML due to the fact I don't think it is simple to use, but I forgot about that sample

    Cheers

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Again I thank you for your help and idea’s but have looked at the sample it appears to be (and please correct me if I am wrong) just another method of storing data in a file this time an XML instead of a text file. Now given that in my code above I have already got a text file reading and writing albeit that’s all I have got how does this move me forward?

    I am going in hospital on Tuesday for (hopefully 10 days for a triple by-pass) and I was trying to finish this before I do but it looks like its going to wait.

    Now if a make it back I will post back then, if someone posts before then I will be working on it until Tuesday, if not well thanks for all of your help
    Last edited by JohnSavage; May 5th, 2007 at 04:23 AM.

  27. #27
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple Database [text file] for Pocket PC

    Quote Originally Posted by JohnSavage
    Again I thank you for your help and idea’s but have looked at the sample it appears to be (and please correct me if I am wrong) just another method of storing data in a file this time an XML instead of a text file. Now given that in my code above I have already got a text file reading and writing albeit that’s all I have got how does this move me forward?
    It moves you forward because it's a class that hides the I/O portion from your main code. You simply refer to a "token" keyword (could be VAR1 or VAR2) and it gets the value from the file (fresh) or writes a new value to the file.

    As far as your code thinks it's simply a function that you pass the "what you want" part to.

    No arrays - no pre-loading - no "make sure you write it before you exit".

    The CLASS hides all this - performs it in the background.

    If you want to use it as PeteVick originally proposed in that link then in code you can say:

    SomeVariable = Get_From_Config_File("Var1")

    and you get the value.

    If you want to change it you can say:

    NewValue = Write_To_Config_File("Var1", NewValue)

    and the file is changed.

    You asked earlier about what a class is - well this is the whole point and beauty of it. It hides the process of what goes on inside from the main code.

    The class is used as:

    SomeVariable = Settings.sServer

    or

    Settings.sServer = "somenewvalue"

    and this calls the class which calls the read and write functions.

    The old brute force method would be when the app starts load an array of items from a file. Maintain that array during the runtime of the app. Then upon exit (or whenever you think it's safest) update the text file.

    With this technique instead you simply have two functions - a READ and a WRITE function. You tell it what value you want and effortlessly that value is available. You want to change the value - and effortlessly the value is changed. And the READ and WRITE functions are further abstracted from main code by a class that has a PROPERTIES exposed that handle the read and write functionality.

    I don't care what method is used in the class to maintain the file - you could have an array in the class - use any kind of I/O method you want. You could change it tomorrow or next week.

    XML was chosen by me because that's one of the few options available on the CE framework. If we had the full framework you would have used the APP.CONFIG functionality and this thread wouldn't even exist!
    Last edited by szlamany; May 5th, 2007 at 06:29 AM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Again thanks for your help, this solution I like the sound of, but as ever I need to get my head around it. I have copied the code from the other post into a new project and created a Component1.vb which I belive is a Class and put the code in it. VB does not like the statement "Imports System.Xml" and appers not to recognise it and displays the error "Type 'XmlTextReader' is not defined" can you help?

    Code:
    Imports System
    Imports System.IO
    Imports System.Xml
    
    Public Class Settings
    
        Private strServer As String
        Private strUserId As String
        Const Datapath = "\Program Files\APC"
    
        Public Property sServer() As String
            Get
                Return strServer
            End Get
            Set(ByVal value As String)
                strServer = Write_To_Config_File("Server", value)
            End Set
        End Property
        Public Property sUserId() As String
            Get
                Return (strUserId)
            End Get
            Set(ByVal value As String)
                strUserId = Write_To_Config_File("UserId", value)
            End Set
        End Property
        Public Sub New()
            strServer = Get_From_Config_File("Server")
            strUserId = Get_From_Config_File("UserId")
        End Sub
    
        Public Function Get_From_Config_File(ByVal strEle As String) As String
            Try
                Dim dr As XmlTextReader
                Dim fs As FileStream = New FileStream(Path.Combine(Datapath, "Config.xml"), FileMode.Open)
                Dim strRet As String
                strRet = ""
                dr = New XmlTextReader(fs)
                While (dr.Read)
                    If dr.NodeType = XmlNodeType.Element And dr.Name = strEle Then
                        strRet = dr.ReadElementString()
                        dr.Close()
                        fs.Close()
                    End If
                End While
                dr.Close()
                fs.Close()
                Return (strRet)
            Catch ex As FileNotFoundException
                Using sw As StreamWriter = New StreamWriter(Path.Combine(Datapath, "Config.xml"))
                    sw.WriteLine("<Configuration_Data>")
                    sw.WriteLine("</Configuration_Data>")
                    sw.Close()
                End Using
                Return ("")
            Catch ex As Exception
                Return ("")
            End Try
        End Function
        Public Function Write_To_Config_File(ByVal strEle As String, ByVal strVal As String) As String
            Write_To_Config_File = ""
            Dim iCt As Integer
            Dim bDone As Boolean
            'Dim appPath As String
            'appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
            Dim xd As XmlDocument = New XmlDocument
            xd.Load(Path.Combine(Datapath, "Config.xml"))
            Dim xe As XmlElement = xd.DocumentElement
            If xe.Name = "Configuration_Data" Then
                For Each xce As XmlElement In xe.ChildNodes
                    If xce.ChildNodes.Count = 1 Then
                        If xce.Name = strEle Then
                            .xce.FirstChild.Value = strVal
                            .bDone = True
                        End If
                        .Else()
                        For iCt = 0 To xce.ChildNodes.Count - 1
                            If xce.ChildNodes(iCt).Name = strEle Then
                                .xce.ChildNodes(iCt).FirstChild.Value = strVal
                                .bDone = True
                            End If
                            .Next()
                    End If
                    If bDone Then
                        Exit For
                    End If
                    .Next()
                    If Not bDone Then
                        Dim elem As XmlElement = xd.CreateElement(strEle)
                        .elem.InnerText = strVal
                        .xe.AppendChild(elem)
                    End If
    	        End If
            '    write the document back to disk
            xd.Save(Path.Combine(Datapath, "Config.xml"))
            Write_To_Config_File = strVal
        End Function
    End Class

  29. #29
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple Database [text file] for Pocket PC

    I am not at work - and only have VB6 on this PC at home...

    There is some reference you need to add to the project - look around the project options and you should find it.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  30. #30
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple Database [text file] for Pocket PC

    I'm curious - how did the ELSE and NEXT statements get changed to

    .ELSE()

    and

    .NEXT()

    That seems pretty odd to me...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  31. #31
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Simple Database [text file] for Pocket PC

    The reference is System.XML.dll ..... Right-click the project in the solution exporer, select "Add Reference" .... on the dialog box that comes up, select hte ".NET" tab (if it doesn't select by default).... scroll down until you find System.XML .... and select it, then Click OK.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Techgnome

    That worked thanks

    Szlamany

    If you look more closely I’am afraid you will find a lot more missing “.” And that was just me trying to get the error messages to go away and not to come back here with just a simple cut and paste error but as before the task is beyond me so here is the code to-date with the reported system errors noted in catpitals, can you help again

    Code:
    Public Class Settings       'CONFLICT IN THE NAMESPACE XML
    
        Private strServer As String
        Private strUserId As String
        Const Datapath = "\Program Files\APC"
    
        Public Property sServer() As String
            Get
                Return strServer
            End Get
            Set(ByVal value As String)
                strServer = Write_To_Config_File("Server", value)
            End Set
        End Property
    
        Public Property sUserId() As String
            Get
                Return strUserId
            End Get
            Set(ByVal value As String)
                strUserId = Write_To_Config_File("UserId", value)
            End Set
        End Property
    
        Public Sub New()
            strServer = Get_From_Config_File("Server")
            strUserId = Get_From_Config_File("UserId")
        End Sub
    
        Public Function Get_From_Config_File(ByVal strEle As String) As String
            Try
                Dim dr As XmlTextReader
                Dim fs As FileStream = New FileStream(Path.Combine(Datapath, "Config.xml"), FileMode.Open)
                Dim strRet As String
                strRet = ""
                dr = New XmlTextReader(fs)
                While dr.Read
                    If dr.NodeType = XmlNodeType.Element And dr.Name = strEle Then
                        strRet = dr.ReadElementString()
                        dr.Close()
                        fs.Close()
                    End If
                End While
                dr.Close()
                fs.Close()
                Return strRet
            Catch ex As FileNotFoundException
                Using sw As StreamWriter = New StreamWriter(Path.Combine(Datapath, "Config.xml"))
                    sw.WriteLine("<Configuration_Data>")
                    sw.WriteLine("</Configuration_Data>")
                    sw.Close()
                End Using
                Return ""
            Catch ex As Exception
                Return ""
            End Try
        End Function
    
        Public Function Write_To_Config_File(ByVal strEle As String, ByVal strVal As String) As String
            Write_To_Config_File = ""
            Dim iCt As Integer
            Dim bDone As Boolean
            'Dim appPath As String
            'appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
            Dim xd As XmlDocument = New XmlDocument
            xd.Load(Path.Combine(Datapath, "Config.xml"))
            Dim xe As XmlElement = xd.DocumentElement
            If xe.Name = "Configuration_Data" Then
                For Each xce As XmlElement In xe.ChildNodes
                    If xce.ChildNodes.Count = 1 Then
                        If xce.Name = strEle Then
                            xce.FirstChild.Value = strVal
                            bDone = True
                        End If
                    Else
                        For iCt = 0 To xce.ChildNodes.Count - 1
                            If xce.ChildNodes(iCt).Name = strEle Then
                                xce.ChildNodes(iCt).FirstChild.Value = strVal
                                bDone = True
                            End If
                        Next
                    End If
                    If bDone Then
                        Exit For
                    End If
                Next
                If Not bDone Then
                    Dim elem As XmlElement = xd.CreateElement(strEle)
                    elem.InnerText = strVal
                    xe.AppendChild(elem)
                End If
            End If
            '    write the document back to disk
            xd.Save(Path.Combine(Datapath, "Config.xml"))
            Write_To_Config_File = strVal
        End Function
    End Class

  33. #33
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple Database [text file] for Pocket PC

    I agree with the namespace conflict. The word "Settings" would tend to make me nervous as a class name, because I would expect it to be used too often. In general, I want names to be descriptive (as that one certainly is), but complicated enough to be unique. After all, thanks to intellisense, you generally don't have to type more than a few characters. Therefore, you could use something like ProjectSettings, or even ProjectSettingsForThisHereDifferentKindOfProject (actually, there is probably a name length limit, but I don't know what it is).


    That's the only CAPITAL error I see. Is there more?
    My usual boring signature: Nothing

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Shaggy Hiker

    The name is now OK thanks

    I have tried again 11 error messages in all and yes I got it wrong last time, sorry



    Code:
    Imports System
    Imports System.IO
    Imports System.Xml
    
    Public Class Settings
    
        Private strServer As String
        Private strUserId As String
        Const Datapath = "\Program Files\APC"
    
        Public Property sServer() As String
            Get
                Return strServer
            End Get
            Set(ByVal value As String)
                strServer = Write_To_Config_File("Server", value)
            End Set
        End Property
        Public Property sUserId() As String
            Get
                Return (strUserId)
            End Get
            Set(ByVal value As String)
                strUserId = Write_To_Config_File("UserId", value)
            End Set
        End Property
        Public Sub New()
            strServer = Get_From_Config_File("Server")
            strUserId = Get_From_Config_File("UserId")
        End Sub
    
        Public Function Get_From_Config_File(ByVal strEle As String) As String
            Try
                Dim dr As XmlTextReader
                Dim fs As FileStream = New FileStream(Path.Combine(Datapath, "Config.xml"), FileMode.Open)
                Dim strRet As String
                strRet = ""
                dr = New XmlTextReader(fs)
                While (dr.Read)
                    If dr.NodeType = XmlNodeType.Element And dr.Name = strEle Then
                        strRet = dr.ReadElementString()
                        dr.Close()
                        fs.Close()
                    End If
                End While
                dr.Close()
                fs.Close()
                Return (strRet)
            Catch ex As FileNotFoundException
                Using sw As StreamWriter = New StreamWriter(Path.Combine(Datapath, "Config.xml"))
                    sw.WriteLine("<Configuration_Data>")
                    sw.WriteLine("</Configuration_Data>")
                    sw.Close()
                End Using
                Return ("")
            Catch ex As Exception
                Return ("")
            End Try
        End Function
        Public Function Write_To_Config_File(ByVal strEle As String, ByVal strVal As String) As String
            Write_To_Config_File = ""
            Dim iCt As Integer
            Dim bDone As Boolean
            'Dim appPath As String
            'appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
            Dim xd As XmlDocument = New XmlDocument
            xd.Load(Path.Combine(Datapath, "Config.xml"))
            Dim xe As XmlElement = xd.DocumentElement
            If xe.Name = "Configuration_Data" Then
                For Each xce As XmlElement In xe.ChildNodes       'FOR MUST END WITH A MATCHING NEXT
                    If xce.ChildNodes.Count = 1 Then
                        If xce.Name = strEle Then
                            .xce.FirstChild.Value = strVal  'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                            .bDone = True                                        'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                        End If
                        .Else()                                                             'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                        For iCt = 0 To xce.ChildNodes.Count - 1       'FOR MUST END WITH A MATCHING NEXT
                            If xce.ChildNodes(iCt).Name = strEle Then
                                .xce.ChildNodes(iCt).FirstChild.Value = strVal   'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                                .bDone = True                'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                            End If
                            .Next()     'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                    End If
                    If bDone Then
                        Exit For
                    End If
                    .Next()             'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                    If Not bDone Then
                        Dim elem As XmlElement = xd.CreateElement(strEle)
                        .elem.InnerText = strVal                'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                        .xe.AppendChild(elem)             'A LEADING "." CAN ONLY APPEAR INSIDE A WITH STATEMENT
                    End If
    	        End If
            '    write the document back to disk
            xd.Save(Path.Combine(Datapath, "Config.xml"))
            Write_To_Config_File = strVal
        End Function
    End Class

  35. #35
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple Database [text file] for Pocket PC

    It looks like somebody was using a With block, then got rid of it, and didn't change it back.

    Copying on this computer sucks, so bear with me, and I will address each line in turn:

    1) Ignore the one on the For line, it will be fixed later.
    2) change .xce.FirstChild.Value = strVal to xce.FirstChild.Value = strVal
    3) change .bDone to bDone (this may cause different errors, but maybe not).
    4) Change .Else() to Else
    5) Another For, and another error to ignore for now. It will go away.
    6) change .xce.Childnodes etc. to xce.Childnodes etc.
    7) change .bDone to bDone (still might cause other errors).
    8) Change .Next() to Next (this will solve error #5)
    9) Same as 8, which will solve error #1.
    10) Change .elem.InnerText to elem.InnerText
    11) Change .xe.AppendChild(elem) to xe.AppendChild(elem)

    That may create new errors, but it will clean up the ones you currently have, so try them out, and post any new ones.
    My usual boring signature: Nothing

  36. #36
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple Database [text file] for Pocket PC

    shaggy...

    look at the link in post #24 - the code is different in that thread...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  37. #37
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple Database [text file] for Pocket PC

    Maybe so, but I am trapped on a mac with a tiny screen. I have only a minimal understanding of how to use this computer, so if there is even a way to have both side by side, I don't know how to do it. On this tiny laptop screen, there wouldn't be much visible anyways. Because of that, I have no rational way to compare the two. Switching between them is painful, and I can't panel the screen.
    My usual boring signature: Nothing

  38. #38

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    First let me apologise to you all it was just me not being able to copy and paste from the example. I have now done it with no reported errors and all of the code is in a new Class called Class1.vb in my project.

    I have then created a simple form to test this code using 4 textboxes and two buttons, my idea is that you press button 1 and the values of textboxes 1 & 2 is copied by the class into the file and if you press button2 then the textboxes 3 & 4 are filled with the original values from textboxes 1 & 2.

    Of course this being me it does not work with the error being "Name Write_To_Config_File and Get_From_Config_File not declared"

    How do I reference the Class? Which is what I think I need to do.


    Code:
    Public Class Form1
        Dim Text1 As String
        Dim Text2 As String
        Dim Text3 As String
        Dim Text4 As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            TextBox1.Text = Write_To_Config_File("text1", newvalue)
            TextBox2.Text = Write_To_Config_File("text2", newvalue)
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            TextBox3.Text = Get_From_Config_File("text1")
            TextBox4.Text = Get_From_Config_File("text2")
        End Sub
    End Class

    ps Szlamany

    Am I right in saying if it was working I could have said in my code

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Write to file
            Text1=TextBox1.Text
            Text2=TextBox2.Text
    
            Settings.sServer=Text1
            Settings.sServer=Text2
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    'read from file
            TextBox3.Text =Settings.sServer(Text1) 
            TextBox4.Text = Settings.sServer(Text2) 
        End Sub
    Or am I wrong again

  39. #39
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple Database [text file] for Pocket PC

    There are basically two ways to utilize this.

    If you put the functions - the READ and WRITE functions - in your form code, for example, you can reference them as you had in this example:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            TextBox1.Text = Write_To_Config_File("text1", newvalue)
            TextBox2.Text = Write_To_Config_File("text2", newvalue)
    
        End Sub
    btw - what are the TEXT1, TEXT2, TEXT3 and TEXT4 variables for??

    The second example - Settings.sServer=Text1 - is using an "sServer" property that I made up. You certainly aren't storing a SERVER name like I was (I guessing) - so if you want to use the class in that manner you create a property for each and every value you want to store - not call them sServer - call them what you want to call them.

    It's entirely up to you - you either use the READ and WRITE functions directly (as in example 1 above) or declare 20 or so properties in the class and reference them in that manner.

    Seems that your needs - writing 20 or so values from a text file - would be just as well served by using the READ and WRITE functions directly.

    It would seem that this error:

    "Name Write_To_Config_File and Get_From_Config_File not declared"

    means you are not putting the functions in the form or declaring them in a fashion public enough to be found.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  40. #40

    Thread Starter
    Fanatic Member
    Join Date
    May 2006
    Posts
    612

    Re: Simple Database [text file] for Pocket PC

    Perhaps I have misunderstood this whole concept, which I have to admit does not surprise me but I am sure it does not amuse you, sorry.

    At the outset I have used your code from the other thread, created a class module and put the code there. The code in this thread is just a simple example of how in the form code I could assign and recover variable contents.

    You say the READ and WRITE functions in my form code can be referenced as I have written them, but as I have said when I do that I get the error "Name Write_To_Config_File and Get_From_Config_File not declared"

    How do I reference them (the functions) in my form code, or do I put all of the code in the form code
    Last edited by JohnSavage; May 6th, 2007 at 09:52 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width