W
wk6pack
Hi,
I have a question about my coding practise. I have a class method to return
a value from a database. I open the connection do my search and dispose the
reader. Open the reader with a new recordset and then close the reader and
close the connection.
I do this for every new record I am adding. But I seem to get an error
around the 770 record to add. The error is and unspecified error at the
connection.open() for that record.
Or should I be opening the connection and leave it open for all the records
I'm adding and just return to the beginning of the recordset to restart the
search?
thanks,
Will
Here is my function code:
Public Function lookupGroup(ByVal lPerson As Person) As String
Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"User ID=Admin;Data Source=" + Path.GetFullPath("EmployeeMatching.mdb")
'W:\Will\Active Directory\Export\EmployeeMatching.mdb"
Dim myReader As OleDbDataReader
Dim catCMD As New OleDbCommand
Dim schooltypecatcode As String
Try
With catCMD
..Connection = New OleDbConnection(sConnString)
..Connection.Open() <******************************
..CommandText = "Select a.schooltypecode from schooltype a, school b where
b.schoolno ='" + lPerson.dept + "' and b.schooltype = a.schooltypeid "
End With
myReader = catCMD.ExecuteReader()
If myReader.HasRows Then
myReader.Read()
schooltypecatcode = myReader.GetValue(0)
Else
schooltypecatcode = Nothing
End If
Catch ex As OleDbException
errorlog(lPerson, ex.Message, " LookupGroup OLEDB:")
Catch e As Exception
errorlog(lPerson, e.Source + ":" + e.Message, " LookupGroup Schooltype:")
Catch
errorlog(lPerson, "Unhandled Exception in schooltypecode lookup. ",
"CatchAll Exception")
Finally
If Not myReader Is Nothing Then myReader.Close()
End Try
If Not schooltypecatcode Is Nothing Then
'get the school type ie. EL for elementary
schooltypecatcode += lPerson.dept
Dim ptype = lPerson.Type
Dim pcat = lPerson.Category
Dim found As Boolean
Try
catCMD.CommandText = "SELECT c.empcatgencode, b.emptypecode,c.empcatcode,
b.emptypedesc, c.empcatdesc FROM EmpTypeCatLink a, emptype b, empcategory c
" + _
" where a.emptypeid = b.emptypeid and a.empcatid = c.empcatid"
myReader = catCMD.ExecuteReader()
Dim schooltype As String
found = False
'cycle thru the table to find the general title ie. TE for teachers
Do While myReader.Read And Not found
If (StrComp(myReader.GetValue(1), ptype) = 0 And
StrComp(myReader.GetValue(2), pcat) = 0) Then
If Not myReader.IsDBNull(0) Then
schooltypecatcode += myReader.GetValue(0)
found = True
Else
schooltypecatcode = Nothing
found = True
End If
End If
Loop
Catch ex As Exception
errorlog(lPerson, ex.Source + ":" + ex.Message, "Lookup Group
Empcatgencode:")
Catch
errorlog(lPerson, "Unhandled Exception: EmpCatGenCode lookup ", "CatchAll
Exception")
Finally
If Not myReader Is Nothing Then myReader.Close()
End Try
End If
If Not catCMD Is Nothing Then catCMD.Dispose()
Return schooltypecatcode
I have a question about my coding practise. I have a class method to return
a value from a database. I open the connection do my search and dispose the
reader. Open the reader with a new recordset and then close the reader and
close the connection.
I do this for every new record I am adding. But I seem to get an error
around the 770 record to add. The error is and unspecified error at the
connection.open() for that record.
Or should I be opening the connection and leave it open for all the records
I'm adding and just return to the beginning of the recordset to restart the
search?
thanks,
Will
Here is my function code:
Public Function lookupGroup(ByVal lPerson As Person) As String
Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"User ID=Admin;Data Source=" + Path.GetFullPath("EmployeeMatching.mdb")
'W:\Will\Active Directory\Export\EmployeeMatching.mdb"
Dim myReader As OleDbDataReader
Dim catCMD As New OleDbCommand
Dim schooltypecatcode As String
Try
With catCMD
..Connection = New OleDbConnection(sConnString)
..Connection.Open() <******************************
..CommandText = "Select a.schooltypecode from schooltype a, school b where
b.schoolno ='" + lPerson.dept + "' and b.schooltype = a.schooltypeid "
End With
myReader = catCMD.ExecuteReader()
If myReader.HasRows Then
myReader.Read()
schooltypecatcode = myReader.GetValue(0)
Else
schooltypecatcode = Nothing
End If
Catch ex As OleDbException
errorlog(lPerson, ex.Message, " LookupGroup OLEDB:")
Catch e As Exception
errorlog(lPerson, e.Source + ":" + e.Message, " LookupGroup Schooltype:")
Catch
errorlog(lPerson, "Unhandled Exception in schooltypecode lookup. ",
"CatchAll Exception")
Finally
If Not myReader Is Nothing Then myReader.Close()
End Try
If Not schooltypecatcode Is Nothing Then
'get the school type ie. EL for elementary
schooltypecatcode += lPerson.dept
Dim ptype = lPerson.Type
Dim pcat = lPerson.Category
Dim found As Boolean
Try
catCMD.CommandText = "SELECT c.empcatgencode, b.emptypecode,c.empcatcode,
b.emptypedesc, c.empcatdesc FROM EmpTypeCatLink a, emptype b, empcategory c
" + _
" where a.emptypeid = b.emptypeid and a.empcatid = c.empcatid"
myReader = catCMD.ExecuteReader()
Dim schooltype As String
found = False
'cycle thru the table to find the general title ie. TE for teachers
Do While myReader.Read And Not found
If (StrComp(myReader.GetValue(1), ptype) = 0 And
StrComp(myReader.GetValue(2), pcat) = 0) Then
If Not myReader.IsDBNull(0) Then
schooltypecatcode += myReader.GetValue(0)
found = True
Else
schooltypecatcode = Nothing
found = True
End If
End If
Loop
Catch ex As Exception
errorlog(lPerson, ex.Source + ":" + ex.Message, "Lookup Group
Empcatgencode:")
Catch
errorlog(lPerson, "Unhandled Exception: EmpCatGenCode lookup ", "CatchAll
Exception")
Finally
If Not myReader Is Nothing Then myReader.Close()
End Try
End If
If Not catCMD Is Nothing Then catCMD.Dispose()
Return schooltypecatcode