Results 1 to 19 of 19

Thread: [RESOLVED] Please solution Object reference not set to an instance of an object.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Resolved [RESOLVED] Please solution Object reference not set to an instance of an object.

    Dear All Master
    I use openfolderdialog for connection to jet.oledb. Please solution.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim FolderBrowserDialog1 As New FolderBrowserDialog()
            FolderBrowserDialog1.SelectedPath = Directory.GetCurrentDirectory
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                TextBox1.Text = FolderBrowserDialog1.SelectedPath
            End If
        End Sub
        Dim Path As String = TextBox1.Text 'Object reference not set to an instance of an object.
        Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
        Private Sub PopulateDataGridView()
            Try
                dt = New DataTable
                Dim query = "select ITM FROM ITEM."
                Using adapter As New OleDbDataAdapter(query, cn.ToString)
                    adapter.Fill(dt)
                End Using
                Me.DataGridView1.DataSource = dt
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    Thanks
    roy88

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Please solution Object reference not set to an instance of an object.

    Code:
    Dim Path As String
    Dim cn As String
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FolderBrowserDialog1 As New FolderBrowserDialog()
        FolderBrowserDialog1.SelectedPath = Directory.GetCurrentDirectory
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
            Path = TextBox1.Text
            cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
        End If
    End Sub
    
    Private Sub PopulateDataGridView()
        Try
            dt = New DataTable
            Dim query = "select ITM FROM ITEM."
            Using adapter As New OleDbDataAdapter(query, cn.ToString)
                adapter.Fill(dt)
            End Using
            Me.DataGridView1.DataSource = dt
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Please solution Object reference not set to an instance of an object.

    Code:
    Using adapter As New OleDbDataAdapter(query, cn)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by .paul. View Post
    Code:
    Using adapter As New OleDbDataAdapter(query, cn)
    dear mr. .paul.
    Thanks for the reply from you, but there's a problem.

    Code:
    Using adapter As New OleDbDataAdapter(query, cn)
                    adapter.Fill(dt) 'The ConnectionString property has not been initialized.
      End Using

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Please solution Object reference not set to an instance of an object.

    Code:
    Dim con As New OleDbConnection(cn)
    Using adapter As New OleDbDataAdapter(query, con)
        adapter.Fill(dt)
    End Using

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by .paul. View Post
    Code:
    Dim con As New OleDbConnection(cn)
    Using adapter As New OleDbDataAdapter(query, con)
        adapter.Fill(dt)
    End Using
    dear mr. .paul.
    Thanks for the reply from you, but still problem.
    Code:
    Dim con As New OleDbConnection(cn)
                Using adapter As New OleDbDataAdapter(query, con)
                    adapter.Fill(dt) 'The ConnectionString property has not been initialized.
                End Using

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

    Re: Please solution Object reference not set to an instance of an object.

    Have you tried stepping through the code and checking thing? Make sure cn is the value it should be. When something isn't right, debugging and stepping through the code should be the first thing you should be doing to diagnose the problem. Have you checked to make sure that the connection string in cn has been constructed correctly? You're fairly new here, but this isn't how it works here. This isn't therapy for you to work out your problems. We should be a last resort, when you can't figure it out. But you first must make an effort. And at the moment, it doesn't look like you are. And saying things like "still problems" isn't much help either. I'll give you credit though, you're posting code and pointing at the line, that's more effort than we usually get from new posters, so I'm willing to work with that, but I'd like to see more of the code... For instance, I don't see how PopulateDataGridView is called -- it doesn't seem to be from the button click -- ... and since the code in question is there, how is that getting called?

    \-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??? *

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Please solution Object reference not set to an instance of an object.

    Funny enough, one of my very first codebank entries does just what you're wanting to do: https://www.vbforums.com/showthread....openfiledialog
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Please solution Object reference not set to an instance of an object.

    Dear All master,

    Below is the full code from me.
    Code:
    Public Class Form1
        Dim dt As New DataTable
        Dim Path As String
        Dim cn As String
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PopulateDataGridView()
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim FolderBrowserDialog1 As New FolderBrowserDialog()
            FolderBrowserDialog1.SelectedPath = Directory.GetCurrentDirectory
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                TextBox1.Text = FolderBrowserDialog1.SelectedPath
                Path = TextBox1.Text
                cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
            End If
        End Sub
        Private Sub PopulateDataGridView()
            Try
                dt = New DataTable
                Dim query = "select ITM FROM ITEM."
                Dim con As New OleDbConnection(cn)
                Using adapter As New OleDbDataAdapter(query, con)
                    adapter.Fill(dt) 'The ConnectionString property has not been initialized.
                End Using
                Me.DataGridView1.DataSource = dt
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    End Class
    for which problem I mark the color red in the code.
    Thanks
    roy88
    Last edited by roy88; Dec 16th, 2021 at 09:52 PM.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Please solution Object reference not set to an instance of an object.

    Code:
    Public Class Form1
        Dim dt As New DataTable
        Dim Path As String
        Dim cn As String
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PopulateDataGridView()
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim FolderBrowserDialog1 As New FolderBrowserDialog()
            FolderBrowserDialog1.SelectedPath = Directory.GetCurrentDirectory
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                TextBox1.Text = FolderBrowserDialog1.SelectedPath            
            End If
        End Sub
        Private Sub PopulateDataGridView()
            Try
                dt = New DataTable
                Dim query = "select ITM FROM ITEM."
                Path = TextBox1.Text
                cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
                Dim con As New OleDbConnection(cn)
                Using adapter As New OleDbDataAdapter(query, con)
                    adapter.Fill(dt) 'The ConnectionString property has not been initialized.
                End Using
                Me.DataGridView1.DataSource = dt
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    End Class

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by .paul. View Post
    Code:
    Public Class Form1
        Dim dt As New DataTable
        Dim Path As String
        Dim cn As String
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PopulateDataGridView()
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim FolderBrowserDialog1 As New FolderBrowserDialog()
            FolderBrowserDialog1.SelectedPath = Directory.GetCurrentDirectory
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                TextBox1.Text = FolderBrowserDialog1.SelectedPath            
            End If
        End Sub
        Private Sub PopulateDataGridView()
            Try
                dt = New DataTable
                Dim query = "select ITM FROM ITEM."
                Path = TextBox1.Text 'error invalid argument
                cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
                Dim con As New OleDbConnection(cn)
                Using adapter As New OleDbDataAdapter(query, con)
                    adapter.Fill(dt) 
                End Using
                Me.DataGridView1.DataSource = dt
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    End Class
    Dear mr..paul.
    Thanks for your reply. But I run the code from you there is an error "invalid argument".
    I mark the color red in the code
    Thanks
    roy88
    Last edited by roy88; Dec 16th, 2021 at 10:27 PM.

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

    Re: Please solution Object reference not set to an instance of an object.

    You need to learn how to use the debugger. If you had done any debugging then the issue would have been obvious. You are using 'cn' in the 'PopulateDataGridView' method and you're calling that from the 'Load' event handler of the form. You're not setting 'cn' until you click 'Button1' though, which obviously happens later. Simple logic should tell you what the problem is but a couple of breakpoints - one where you set 'cn' and one where you use 'cn' - would have demonstrated it clearly. You should not be posting a question here without having debugged your code first. If you don't know how to use the debugger, stop what you're doing and learn. It is essential knowledge for every developer, regardless of skill level.
    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

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by jmcilhinney View Post
    You need to learn how to use the debugger. If you had done any debugging then the issue would have been obvious. You are using 'cn' in the 'PopulateDataGridView' method and you're calling that from the 'Load' event handler of the form. You're not setting 'cn' until you click 'Button1' though, which obviously happens later. Simple logic should tell you what the problem is but a couple of breakpoints - one where you set 'cn' and one where you use 'cn' - would have demonstrated it clearly. You should not be posting a question here without having debugged your code first. If you don't know how to use the debugger, stop what you're doing and learn. It is essential knowledge for every developer, regardless of skill level.
    The problem in the code below
    Code:
    Path = TextBox1.Text 'error invalid argument

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Please solution Object reference not set to an instance of an object.

    That is NOT the line causing the error. As jmcilhinney told you, you’re running your code when TextBox1.Text = “”
    Therefore cn is invalid…

    You should do as jmcilhinney told you to. You’re really not helping yourself…

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

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by roy88 View Post
    The problem in the code below
    Code:
    Path = TextBox1.Text 'error invalid argument
    You just ignored everything I posted previously. The issue is exactly the same, i.e. you're trying to use data that you haven't actually got yet. Instead of blindly copying and pasting code that others write, how about you put some thought and effort into this for yourself? The fact that I told you asked about effectively the exact same problem that I already told you how to resolve shows that you're not doing either.
    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

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by .paul. View Post
    That is NOT the line causing the error. As jmcilhinney told you, you’re running your code when TextBox1.Text = “”
    Therefore cn is invalid…

    You should do as jmcilhinney told you to. You’re really not helping yourself…
    Dear Mr. .paul.
    what you say is true, so what is the solution to avoid empty textbox1.text If I keep using button1 for FolderBrowserDialog.Below is the full code from me without using button1 succeeds.
    Code:
    Public Class Form1
        Dim dt As New DataTable
        Dim Path As String
        Dim cn As String
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If File.Exists(Path) = False Then
                MessageBox.Show("Do you want to change path of the database?", "System Message", MessageBoxButtons.OK)
                Dim FolderBrowserDialog1 As New FolderBrowserDialog()
                FolderBrowserDialog1.SelectedPath = Directory.GetCurrentDirectory
                If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                    TextBox1.Text = FolderBrowserDialog1.SelectedPath
                End If
            End If
            PopulateDataGridView()
        End Sub
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        End Sub
        Private Sub PopulateDataGridView()
            Try
                dt = New DataTable
                Path = TextBox1.Text
                cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    
                Dim query = "select ITM FROM ITEM."
                Dim con As New OleDbConnection(cn)
    
                Using adapter As New OleDbDataAdapter(query, con)
                    adapter.Fill(dt)
                End Using
                Me.DataGridView1.DataSource = dt
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    
    End Class
    thanks
    roy88

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

    Re: Please solution Object reference not set to an instance of an object.

    You're still not really putting anything into this. You should be working out the logic well before writing any code but you have no idea what the logic is here. Think about it! If the user needs to supply information in order for the application to connect to the database, how can the application possibly connect to the database before the user has even seen a form in order to supply that information? It makes no sense so why are you writing code to do it? If the grid requires you to connect to the database and connecting the database requires input from the user then you can't possibly populate the grid before the user has provided that input, so don't write code to do it and then be amazed that it doesn't work. Work out the logic first, then write code to implement that logic. If you do that then your code won't try to do illogical things.
    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

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by roy88 View Post
    Private Sub PopulateDataGridView()
    Try
    dt = New DataTable
    Path = TextBox1.Text
    cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"

    Stop
    'inspect cn here. Be sure you have the right connection string
    'examples of connection strings are available at connectionstrings.com


    Dim query = "select ITM FROM ITEM."
    Dim con As New OleDbConnection(cn)

    Using adapter As New OleDbDataAdapter(query, con)
    adapter.Fill(dt)
    End Using
    Me.DataGridView1.DataSource = dt
    Catch myerror As OleDbException
    MessageBox.Show("Error: " & myerror.Message)
    Finally
    End Try
    End Sub
    See the notes in the code snippet

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Please solution Object reference not set to an instance of an object.

    Quote Originally Posted by .paul. View Post
    See the notes in the code snippet
    dear Mr. .paul.

    Thank you for your reply, sorry I'm late replying.
    thanks
    roy88

Tags for this Thread

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