|
-
Dec 15th, 2021, 09:55 PM
#1
Thread Starter
Lively Member
[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
-
Dec 15th, 2021, 10:01 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 15th, 2021, 10:04 PM
#3
Re: Please solution Object reference not set to an instance of an object.
Code:
Using adapter As New OleDbDataAdapter(query, cn)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 15th, 2021, 10:17 PM
#4
Thread Starter
Lively Member
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by .paul.
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
-
Dec 15th, 2021, 10:26 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 15th, 2021, 10:41 PM
#6
Thread Starter
Lively Member
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by .paul.
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
-
Dec 16th, 2021, 08:49 AM
#7
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
-
Dec 16th, 2021, 09:34 AM
#8
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
-
Dec 16th, 2021, 09:48 PM
#9
Thread Starter
Lively Member
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.
-
Dec 16th, 2021, 09:54 PM
#10
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 16th, 2021, 10:02 PM
#11
Thread Starter
Lively Member
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by .paul.
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.
-
Dec 16th, 2021, 10:05 PM
#12
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.
-
Dec 16th, 2021, 10:28 PM
#13
Thread Starter
Lively Member
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by jmcilhinney
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
-
Dec 16th, 2021, 10:49 PM
#14
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 16th, 2021, 10:53 PM
#15
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by roy88
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.
-
Dec 16th, 2021, 11:01 PM
#16
Thread Starter
Lively Member
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by .paul.
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
-
Dec 17th, 2021, 12:16 AM
#17
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.
-
Dec 17th, 2021, 10:55 AM
#18
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by roy88
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 20th, 2021, 07:53 AM
#19
Thread Starter
Lively Member
Re: Please solution Object reference not set to an instance of an object.
 Originally Posted by .paul.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|