gr8life
asked on
Problem OccurredObject reference not set to an instance of an object
I am trying to split the function IPlookup in to two separate functions. The splitting of the functions is an attempt to reduce the amount of times the application has to go to the database by populating a datatable and selecting information from it. This approach was suggested as a performance enhancement in a post I made for suggestions on performance enhancements. Please see the URL below for questions:
https://www.experts-exchange.com/questions/21784599/Performance-Improvements.html
I attempted several different approaches, but am still struggling to make it work.
I’m getting an error “Problem OccurredObject reference not set to an instance of an object.”
Original code:
Public Function IPLookup(ByVal DottedIP As String) As String
Dim lngIP As Long
Dim strCountry As String
Dim con As New OleDbConnection
Dim dtm As DataTableMapping
Dim da As New OleDbDataAdapter
Dim cmdSelect As New OleDbCommand
Dim ds As New DataSet
Dim dt As DataTable
lngIP = Dot2LongIP(DottedIP)
con.ConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source = C:\resource\master.mdb"
dtm = da.TableMappings.Add("Tabl e", "Country")
cmdSelect.CommandText = "SELECT countryname FROM Country WHERE " & lngIP.ToString & " >= ipl1 and " _
& lngIP.ToString & " <= ipl2"
cmdSelect.Connection = con
da.SelectCommand = cmdSelect
da.Fill(ds)
dt = ds.Tables("Country")
If dt.Rows.Count = 0 Then
strCountry = "Not is Database!"
Else
strCountry = dt.Rows(0).Item("countryna me")
End If
Return strCountry
End Function
End Class
Split code
Partial code (I included this partial code section because I wasn't sure if this was what was causing the error)
If (inFile.Length > 0) Then
If (outFile.Length > 0) Then
CountryDatatable() 'I added call to the datatable
ConvertFiles(inFile, outFile)
'Wave.Play("C:\resource\so unds\compl eted.wav")
MsgBox("File Complete!")
Else
Here is the modified code:
Public Function CountryDatatable()
Dim cmdSelect As New OleDbCommand
Dim dtm As DataTableMapping
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Dim con As New OleDbConnection
con.ConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source = C:\resource\master.mdb"
cmdSelect.CommandText = "SELECT * from Country"
dtm = da.TableMappings.Add("Tabl e", "Country")
cmdSelect.Connection = con
da.SelectCommand = cmdSelect
da.Fill(ds)
dt = ds.Tables("Country")
End Function
Public Function IPLookup(ByVal DottedIP As String) As String
Dim lngIP As Long
Dim strCountry As String
Dim ds As DataSet
Dim dt As DataTable
dt = ds.Tables("Country")
lngIP = Dot2LongIP(DottedIP)
Dim sql As String
Dim foundrows() As DataRow
sql = lngIP.ToString & ">=ipl1 and " & lngIP.ToString & " <= ipl2"
foundrows = dt.Select(sql)
If foundrows.Length = 0 Then
strCountry = "unknown"
Else
strCountry = foundrows(0).Item("country name")
End If
Return strCountry
End Function
Thank you very much for your time and expertise,
Gr8life
https://www.experts-exchange.com/questions/21784599/Performance-Improvements.html
I attempted several different approaches, but am still struggling to make it work.
I’m getting an error “Problem OccurredObject reference not set to an instance of an object.”
Original code:
Public Function IPLookup(ByVal DottedIP As String) As String
Dim lngIP As Long
Dim strCountry As String
Dim con As New OleDbConnection
Dim dtm As DataTableMapping
Dim da As New OleDbDataAdapter
Dim cmdSelect As New OleDbCommand
Dim ds As New DataSet
Dim dt As DataTable
lngIP = Dot2LongIP(DottedIP)
con.ConnectionString = "Provider=Microsoft.Jet.OL
dtm = da.TableMappings.Add("Tabl
cmdSelect.CommandText = "SELECT countryname FROM Country WHERE " & lngIP.ToString & " >= ipl1 and " _
& lngIP.ToString & " <= ipl2"
cmdSelect.Connection = con
da.SelectCommand = cmdSelect
da.Fill(ds)
dt = ds.Tables("Country")
If dt.Rows.Count = 0 Then
strCountry = "Not is Database!"
Else
strCountry = dt.Rows(0).Item("countryna
End If
Return strCountry
End Function
End Class
Split code
Partial code (I included this partial code section because I wasn't sure if this was what was causing the error)
If (inFile.Length > 0) Then
If (outFile.Length > 0) Then
CountryDatatable() 'I added call to the datatable
ConvertFiles(inFile, outFile)
'Wave.Play("C:\resource\so
MsgBox("File Complete!")
Else
Here is the modified code:
Public Function CountryDatatable()
Dim cmdSelect As New OleDbCommand
Dim dtm As DataTableMapping
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Dim con As New OleDbConnection
con.ConnectionString = "Provider=Microsoft.Jet.OL
cmdSelect.CommandText = "SELECT * from Country"
dtm = da.TableMappings.Add("Tabl
cmdSelect.Connection = con
da.SelectCommand = cmdSelect
da.Fill(ds)
dt = ds.Tables("Country")
End Function
Public Function IPLookup(ByVal DottedIP As String) As String
Dim lngIP As Long
Dim strCountry As String
Dim ds As DataSet
Dim dt As DataTable
dt = ds.Tables("Country")
lngIP = Dot2LongIP(DottedIP)
Dim sql As String
Dim foundrows() As DataRow
sql = lngIP.ToString & ">=ipl1 and " & lngIP.ToString & " <= ipl2"
foundrows = dt.Select(sql)
If foundrows.Length = 0 Then
strCountry = "unknown"
Else
strCountry = foundrows(0).Item("country
End If
Return strCountry
End Function
Thank you very much for your time and expertise,
Gr8life
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm trying the code solution provided by BriCrowe and I'm getting an underlined error 'tablemapping" is not a member of the 'System.Data.OleDbDataAdap ter'
In this row
.tablemapping.add("Table", "Country")
Any suggestions,
Thank you very much!
Gr8life
In this row
.tablemapping.add("Table",
Any suggestions,
Thank you very much!
Gr8life
ASKER
Also appari your are exactly right!
I am trying to call CountryDatatable() once and prepare the datatable and use it in IPLookup function many times in a loop.
Thank you for time,
Gr8life
I am trying to call CountryDatatable() once and prepare the datatable and use it in IPLookup function many times in a loop.
Thank you for time,
Gr8life
ASKER
Here is the code where the CountryDatatable() is being called.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inFile As String
Dim outFile As String
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDir ectory = "c:\resource"
openFileDialog1.Filter = "txt files (*.txt|*.txt|All files(*.*)|*.*"
openFileDialog1.FilterInde x = 2
openFileDialog1.RestoreDir ectory = True
If openFileDialog1.ShowDialog () = DialogResult.OK Then
inFile = openFileDialog1.FileName
outFile = Mid(inFile, 1, inFile.LastIndexOf("."))
outFile += "-out.CSV"
End If
If (inFile.Length > 0) Then
If (outFile.Length > 0) Then
CountryDatatable() 'Here is where I am calling function
ConvertFiles(inFile, outFile)
'Wave.Play("C:\resource\so unds\compl eted.wav")
MsgBox("File Complete!")
Else
MsgBox("No output file specified!")
End If
Else
MsgBox("No input file specified!")
End If
End Sub
Thank you very much,
Gr8life
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inFile As String
Dim outFile As String
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDir
openFileDialog1.Filter = "txt files (*.txt|*.txt|All files(*.*)|*.*"
openFileDialog1.FilterInde
openFileDialog1.RestoreDir
If openFileDialog1.ShowDialog
inFile = openFileDialog1.FileName
outFile = Mid(inFile, 1, inFile.LastIndexOf("."))
outFile += "-out.CSV"
End If
If (inFile.Length > 0) Then
If (outFile.Length > 0) Then
CountryDatatable() 'Here is where I am calling function
ConvertFiles(inFile, outFile)
'Wave.Play("C:\resource\so
MsgBox("File Complete!")
Else
MsgBox("No output file specified!")
End If
Else
MsgBox("No input file specified!")
End If
End Sub
Thank you very much,
Gr8life
did you try the code i posted in my second post?
i was doing it off the cuff...it should be
.tablemappings.add("Table" , "Country")
.tablemappings.add("Table"
ASKER
I tried both solutions and they both work great. However I am receiving an error “Problem Occurred Index was outside the bounds of the array” I believe it is because of the way I am calling the CountryDatatable function. Any suggestions?
Thanks for your time and expertise,
Gr8life
Thanks for your time and expertise,
Gr8life
can you run through it in debug mode and nail down which line of code is throwing the error?
ASKER
here is the output of the debug
'DefaultDomain': Loaded 'c:\windows\microsoft.net\ framework\ v1.1.4322\ mscorlib.d ll', No symbols loaded.
'performance testing': Loaded 'C:\Documents and Settings\home\My Documents\Visual Studio Projects\performance testing\bin\performance testing.exe', Symbols loaded.
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s ystem.wind ows.forms\ 1.0.5000.0 __b77a5c56 1934e089\s ystem.wind ows.forms. dll', No symbols loaded.
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s ystem\1.0. 5000.0__b7 7a5c561934 e089\syste m.dll', No symbols loaded.
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s ystem.data \1.0.5000. 0__b77a5c5 61934e089\ system.dat a.dll', No symbols loaded.
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s ystem.xml\ 1.0.5000.0 __b77a5c56 1934e089\s ystem.xml. dll', No symbols loaded.
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s ystem.draw ing\1.0.50 00.0__b03f 5f7f11d50a 3a\system. drawing.dl l', No symbols loaded.
'performance testing.exe': Loaded 'c:\windows\assembly\gac\m icrosoft.v isualbasic \7.0.5000. 0__b03f5f7 f11d50a3a\ microsoft. visualbasi c.dll', No symbols loaded.
The program '[3776] performance testing.exe' has exited with code 0 (0x0).
'DefaultDomain': Loaded 'c:\windows\microsoft.net\
'performance testing': Loaded 'C:\Documents and Settings\home\My Documents\Visual Studio Projects\performance testing\bin\performance testing.exe', Symbols loaded.
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s
'performance testing.exe': Loaded 'c:\windows\assembly\gac\s
'performance testing.exe': Loaded 'c:\windows\assembly\gac\m
The program '[3776] performance testing.exe' has exited with code 0 (0x0).
unfortunately that doesn't help much. It would be much more useful if you could just step through the code and determine which line is throwing the error.
>>I tried both solutions and they both work great.
>>However I am receiving an error “Problem Occurred Index was outside the bounds of the array” I believe it is because of the way I am calling the CountryDatatable function.
what does this mean, first statement says it works and next you are saying you are getting error.
just add try catch blocks to catch the exceptions and display proper error as follows
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
try
Dim inFile As String
Dim outFile As String
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDir ectory = "c:\resource"
openFileDialog1.Filter = "txt files (*.txt|*.txt|All files(*.*)|*.*"
openFileDialog1.FilterInde x = 2
openFileDialog1.RestoreDir ectory = True
If openFileDialog1.ShowDialog () = DialogResult.OK Then
inFile = openFileDialog1.FileName
outFile = Mid(inFile, 1, inFile.LastIndexOf("."))
outFile += "-out.CSV"
End If
If (inFile.Length > 0) Then
If (outFile.Length > 0) Then
CountryDatatable() 'Here is where I am calling function
ConvertFiles(inFile, outFile)
'Wave.Play("C:\resource\so unds\compl eted.wav")
MsgBox("File Complete!")
Else
MsgBox("No output file specified!")
End If
Else
MsgBox("No input file specified!")
End If
catch ex as exception
MsgBox("Error in Button1_Click :" & ex.description)
end try
End Sub
>>However I am receiving an error “Problem Occurred Index was outside the bounds of the array” I believe it is because of the way I am calling the CountryDatatable function.
what does this mean, first statement says it works and next you are saying you are getting error.
just add try catch blocks to catch the exceptions and display proper error as follows
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
try
Dim inFile As String
Dim outFile As String
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDir
openFileDialog1.Filter = "txt files (*.txt|*.txt|All files(*.*)|*.*"
openFileDialog1.FilterInde
openFileDialog1.RestoreDir
If openFileDialog1.ShowDialog
inFile = openFileDialog1.FileName
outFile = Mid(inFile, 1, inFile.LastIndexOf("."))
outFile += "-out.CSV"
End If
If (inFile.Length > 0) Then
If (outFile.Length > 0) Then
CountryDatatable() 'Here is where I am calling function
ConvertFiles(inFile, outFile)
'Wave.Play("C:\resource\so
MsgBox("File Complete!")
Else
MsgBox("No output file specified!")
End If
Else
MsgBox("No input file specified!")
End If
catch ex as exception
MsgBox("Error in Button1_Click :" & ex.description)
end try
End Sub
sorry chaneg
MsgBox("Error in Button1_Click :" & ex.description)
to
MsgBox("Error in Button1_Click :" & ex.message)
MsgBox("Error in Button1_Click :" & ex.description)
to
MsgBox("Error in Button1_Click :" & ex.message)
ASKER
Same problem occurred after I added I the changes provided by appari. I really appreciate all the help the two have you have given me. I’m going to close this question and open another one with this problem. So if you want to continue to help I would greatly appreciate it. I wish I could have provided more points but the limit is 500. Thank you very much for all your hard work and expertise.
Thank you for your valuable time,
Gr8life
Thank you for your valuable time,
Gr8life
ASKER
The thing that doesn't make the most sense is that the application finishes processing all the IP lookups and then it seams like it wants to continue through the looping. Then it is almost like it errors because it can't stop the looping any other way.
Gr8life
Gr8life
where are you getting the error?
I think on this line
dt = ds.Tables("Country")
in IPLookup function?
thats because ds is not yet created.
where are you calling function CountryDatatable()?
i think you are trying to call CountryDatatable() once and prepare the datatable and use it in IPLookup function many times in a loop. if thats the case remove
Dim ds As DataSet
Dim dt As DataTable
dt = ds.Tables("Country") from IPLookup function
and shift Dim dt As New DataTable from CountryDatatable() to module level
and try