Results 1 to 10 of 10

Thread: Trouble with my simple database loading and saving data

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Trouble with my simple database loading and saving data

    Hey im having an issue with my program loading and saving data. My program has 6 textboxes where you enter data and then it is added into a list box where it is then displayed.
    Currently my program is giving me 3 errors :
    Error Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
    Error 2 Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'.
    'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.

    all relating to these pieces of code:
    Code:
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
            OpenFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            OpenFileDialog1.ShowDialog()
        End Sub
    
        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            SaveFileDialog1.ShowDialog()
    
        End Sub
    Code:
     Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
            Try
                Dim sr As StreamReader = New StreamReader(OpenFileDialog1.filename)
                Dim count As Integer = Integer.Parse(sr.ReadLine())
                data.Clear()
                SimpleDatabase.Items.Clear()
    
                Dim tempstock As Stock
                tempstock.name = sr.ReadLine()
                tempstock.catagory = sr.ReadLine()
                tempstock.location = sr.ReadLine()
                tempstock.price = sr.ReadLine()
                tempstock.order = Integer.Parse(sr.ReadLine())
                tempstock.stock = Integer.Parse(sr.ReadLine())
                data.Add(tempstock)
                SimpleDatabase.Items.Add(tempstock.name)
    
                sr.Close()
            Catch ex As Exception
                Console.WriteLine("the file could not be read:")
                Console.Write(ex.Message)
    
            End Try
        End Sub
        Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
            Dim filetosave As String = savefiledialog1.filename
            Dim save As New System.IO.StreamWriter(filetosaveas)
    
            save.WriteLine(data.Count)
    
            Dim tempstock As Stock = data.Item(1)
            save.WriteLine(tempstock.name)
            save.WriteLine(tempstock.catagory)
            save.WriteLine(tempstock.location)
            save.WriteLine(tempstock.price)
            save.WriteLine(tempstock.order)
            save.WriteLine(tempstock.stock)
    
            save.Close()
    
    
    
        End Sub
    This is a school project and my teacher is less than helpful so please anyone i need help, if it is helpful i can send a copy of the program through email and that may help as a reference.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trouble with my simple database loading and saving data

    The first error indicates that one of your event handlers is handling a method for a non-existent variable or one not declared WithEvents. That means one of these:
    Code:
    Handles btnLoad.Click
    Code:
    Handles btnSave.Click
    Code:
    Handles OpenFileDialog1.FileOk
    Code:
    Handles SaveFileDialog1.FileOk
    It might help if you indicated exactly where the error occurs.

    The second one is probably here:
    Code:
            Dim filetosave As String = savefiledialog1.filename
            Dim save As New System.IO.StreamWriter(filetosaveas)
    I'm guessing that that second line is supposed to be using the variable declared in the first line. Why do you have a 'filetosaveas' variable declared at the class level as an Object anyway? If you were to use proper camel-casing, e.g. 'fileToSaveAs' then a mistake like that would be easier to spot.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Trouble with my simple database loading and saving data

    sorry i didnt specify the actual problem. i feel like an idiot...
    The lines with the problem are these
    Code:
    Handles OpenFileDialog1.FileOk
    Code:
    New StreamReader(OpenFileDialog1.filename)
    Code:
    savefiledialog1.FileOk
    The stream reader code is having the error message of
    Error 2 Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'.
    'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Trouble with my simple database loading and saving data

    The error message is quite clear as to what the problem is. You are using variables typed as Object in situations where strongly typed variables are required. In subs and functions you must use the specific type required. Object is simply too vague a type to be interpreted correctly. If a String is needed, use a String variable.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trouble with my simple database loading and saving data

    Did you add an OpenFileDialog and a SaveFileDialog to your form in the designer? If so, did you then remove them and declare variables with the same names yourself in code? Did you maybe copy and paste that code from somewhere without adding the required components in the designer?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Trouble with my simple database loading and saving data

    Thanks alot that fixed the problem.. i totally forgot i had to do that.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Trouble with my simple database loading and saving data

    ok now when i try to save the data i have entered it breaks at this line:
    Code:
     Dim filetosave As String = savefiledialog1.filename
            Dim save As New System.IO.StreamWriter(filetosaveas)
    
            save.WriteLine(data.Count)
    
            Dim tempstock As Stock = data.Item(1)
            save.WriteLine(tempstock.name)
            save.WriteLine(tempstock.catagory)
            save.WriteLine(tempstock.location)
            save.WriteLine(tempstock.price)
            save.WriteLine(tempstock.order)
            save.WriteLine(tempstock.stock)
    
            save.Close()
    
    
    
        End Sub
    Have i forgotten something?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trouble with my simple database loading and saving data

    Quote Originally Posted by Uncookedparsnip View Post
    ok now when i try to save the data i have entered it breaks at this line:
    Code:
     Dim filetosave As String = savefiledialog1.filename
            Dim save As New System.IO.StreamWriter(filetosaveas)
    
            save.WriteLine(data.Count)
    
            Dim tempstock As Stock = data.Item(1)
            save.WriteLine(tempstock.name)
            save.WriteLine(tempstock.catagory)
            save.WriteLine(tempstock.location)
            save.WriteLine(tempstock.price)
            save.WriteLine(tempstock.order)
            save.WriteLine(tempstock.stock)
    
            save.Close()
    
    
    
        End Sub
    Have i forgotten something?
    Most likely. You might want to go back and read post #2 again. If that doesn't answer your question then you might explain in more detail what you mean by "breaks".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    7

    Re: Trouble with my simple database loading and saving data

    When i run the program and try to load my text file. It stops and specify's the line i underlined above. Meaning that there is something conflicting there.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trouble with my simple database loading and saving data

    It doesn't just stop. DETAILS! If there's an error then there's an error message. Keeping that a secret is not the way to get help. That said, I referred to that same line back in post #2. Why are we still on that? I told you what I thought the issue is. Either that is the issue and you were told days ago how to fix it or that is not the issue but you haven;t bothered to explain what you're actually trying to achieve there. Are those two lines supposed to be using the same variable? If so then why did you ignore my earlier advice? If not then why not because it doesn't seem to make sense otherwise?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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