I am developing a custom application for Microstation v8. My app connects to an Access database (I haven't yet hooked it up to Microstation).
Within my code I have two very similar functions to query a database for a particular record. They are:
Public Function GetProjCfgFromDb(ByVal lProjID As Long) As String
Try
'connection stuff
Dim Conn As New OleDbConnection(sConnString & dbFullPath)
Dim strSQL As String = "SELECT proj_cfg FROM tbl_projects WHERE proj_id = " & lProjID
Dim Cmd As New OleDbCommand(strSQL, Conn)
Conn.Open()
GetProjCfgFromDb = CType(Cmd.ExecuteScalar(), String)
'cleanup
Conn.Dispose()
Conn = Nothing
Cmd.Dispose()
Cmd = Nothing
Catch ex As Exception
MsgBox(ex.Source & ex.Message, MsgBoxStyle.Critical)
End Try
End Function
Public Function GetProjIDFromDb(ByVal sProjCfg As String) As Long
Try
'connection stuff
Dim Conn As New OleDbConnection(sConnString & dbFullPath)
Dim strSQL As String = "SELECT proj_id FROM tbl_projects WHERE proj_cfg = " & sProjCfg
Dim Cmd As New OleDbCommand(strSQL, Conn)
Conn.Open()
GetProjIDFromDb = CType(Cmd.ExecuteScalar(), Long)
'cleanup
Conn.Dispose()
Conn = Nothing
Cmd.Dispose()
Cmd = Nothing
Catch ex As Exception
MsgBox(ex.Source & ex.Message, MsgBoxStyle.Critical)
End Try
End Function
This first function is called like this:
Dim sProjConfig As String
sProjConfig = GetProjCfgFromDb(CLng(Me.cboProjects.SelectedValue))
and works perfectly, but the second function is called like this:
Private Sub CheckForProject()
If IsProcessExecuting("ustation") = False Then
projectForm = New frmProj
projectForm.ShowDialog()
Else
'process is executing so get the project
'configuration file currently selected
'and set project ID from database
oMSTN = New MicrostationDGN.Application
Dim sConfigName As String
sConfigName = oMSTN.ActiveWorkspace.ConfigurationVariableValue("_USTN_PROJECTNAME")
lProj_ID = GetProjIDFromDb(sConfigName)
End If
End Sub
where oMSTN.ActiveWorkspace.ConfigurationVariableValue("_USTN_PROJECTNAME") returns a string (eg "SRA 1957 BGS Lower School").
GetProjIDFromDb() fails ion this line:
GetProjIDFromDb = CType(Cmd.ExecuteScalar(), Long)
I have checked all the obvious things but I cannot see what is causing the error.
Please help
Russ
Regards,
Russ
Within my code I have two very similar functions to query a database for a particular record. They are:
Public Function GetProjCfgFromDb(ByVal lProjID As Long) As String
Try
'connection stuff
Dim Conn As New OleDbConnection(sConnString & dbFullPath)
Dim strSQL As String = "SELECT proj_cfg FROM tbl_projects WHERE proj_id = " & lProjID
Dim Cmd As New OleDbCommand(strSQL, Conn)
Conn.Open()
GetProjCfgFromDb = CType(Cmd.ExecuteScalar(), String)
'cleanup
Conn.Dispose()
Conn = Nothing
Cmd.Dispose()
Cmd = Nothing
Catch ex As Exception
MsgBox(ex.Source & ex.Message, MsgBoxStyle.Critical)
End Try
End Function
Public Function GetProjIDFromDb(ByVal sProjCfg As String) As Long
Try
'connection stuff
Dim Conn As New OleDbConnection(sConnString & dbFullPath)
Dim strSQL As String = "SELECT proj_id FROM tbl_projects WHERE proj_cfg = " & sProjCfg
Dim Cmd As New OleDbCommand(strSQL, Conn)
Conn.Open()
GetProjIDFromDb = CType(Cmd.ExecuteScalar(), Long)
'cleanup
Conn.Dispose()
Conn = Nothing
Cmd.Dispose()
Cmd = Nothing
Catch ex As Exception
MsgBox(ex.Source & ex.Message, MsgBoxStyle.Critical)
End Try
End Function
This first function is called like this:
Dim sProjConfig As String
sProjConfig = GetProjCfgFromDb(CLng(Me.cboProjects.SelectedValue))
and works perfectly, but the second function is called like this:
Private Sub CheckForProject()
If IsProcessExecuting("ustation") = False Then
projectForm = New frmProj
projectForm.ShowDialog()
Else
'process is executing so get the project
'configuration file currently selected
'and set project ID from database
oMSTN = New MicrostationDGN.Application
Dim sConfigName As String
sConfigName = oMSTN.ActiveWorkspace.ConfigurationVariableValue("_USTN_PROJECTNAME")
lProj_ID = GetProjIDFromDb(sConfigName)
End If
End Sub
where oMSTN.ActiveWorkspace.ConfigurationVariableValue("_USTN_PROJECTNAME") returns a string (eg "SRA 1957 BGS Lower School").
GetProjIDFromDb() fails ion this line:
GetProjIDFromDb = CType(Cmd.ExecuteScalar(), Long)
I have checked all the obvious things but I cannot see what is causing the error.
Please help
Russ
Regards,
Russ