Asked by:
Customer application unable to connect to database and throw error " operation is not allowed when the object is closed"

Question
-
Hi All,
I have custom application that unable to connect to database and giving error below. I am using SQl express with 2017 console. Let me know whether you need any more information to help me resolve this issues. Thanks.
Here is the code
Imports ADODB
Imports System.Data.OleDb
Imports System.IO
Imports System.Data
Public Class DatabaseCn
Public strDBDir As String
Public Cn As Connection
Public cmdSP As Command
Public Rst As Recordset
Public Rst1 As Recordset
Public Rst2 As Recordset
Public Rst3 As Recordset
Public da As OleDbDataAdapter
Public ds As DataSet
Public dt As DataTable
Public CnMode As Integer = ConnectModeEnum.adModeUnknown
Public cmdType As Integer = CommandTypeEnum.adCmdStoredProc
Public CnStr As String
Public Sql As String
Public Sub New()
Cn = New Connection
cmdSP = New Command
Rst = New Recordset
Rst1 = New Recordset
Rst2 = New Recordset
Rst3 = New Recordset
da = New OleDbDataAdapter
ds = New DataSet
dt = New DataTable
'***SQL***
'Cn.CursorLocation = CursorLocationEnum.adUseServer
Cn.CursorLocation = CursorLocationEnum.adUseClient
CnStr = "Provider=sqloledb; Data Source=" & objSQL.Server & "; Initial Catalog=" & objSQL.Database & "; " _
& "User ID=" & objSQL.UserID & "; Password=" & objSQL.UserPwd & ";"
End Sub
Public Sub Open()
Cn.Open(CnStr, objSQL.UserID, objSQL.UserPwd, CnMode)
End Sub
Public Sub Execute(ByVal strSql As String)
Rst = Cn.Execute(strSql)
End Sub
Public Sub Execute1(ByVal strSql As String)
Rst1 = Cn.Execute(strSql)
End Sub
Public Sub Execute2(ByVal strSql As String)
Rst2 = Cn.Execute(strSql)
End Sub
Public Sub Execute3(ByVal strSql As String)
Rst3 = Cn.Execute(strSql)
End Sub
Public Sub Close()
Cn.Close()
End Sub
End ClassRegards,
Siva Kumar
Siva
Friday, August 14, 2020 2:10 AM
All replies
-
Siva
Friday, August 14, 2020 2:10 AM -
I have modified to add time out but still not working
Public Sub Open()
Cn.ConnectionTimeout = 30
Cn.Open(CnStr, objSQL.UserID, objSQL.UserPwd, CnMode)
End SubSiva
Friday, August 14, 2020 2:16 AM -
I trace my VB code and i found "Login timeout expired" when i run the application in VB.Net
Siva
Friday, August 14, 2020 2:26 AM -
Hi Siva,
Please refer below links for more details and check whether they are helpful to you.
Open SQL Server database by using SQL Server .NET Data Provider with Visual Basic .NET
We still recommend you to post your question in related VB.net forum for more professional help from many experts.
Best regards
Melissa
""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
For more information, please refer to the sticky post.Friday, August 14, 2020 2:54 AM -
Don't build your connection string by concatenation. That can lead to the user injecting nasty stuff. Use the ConnectiongString builder class,
https://docs.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbconnectionstringbuilder?view=dotnet-plat-ext-3.1By the way, why are you using OleDb client and not SqlClient? Do you need to support other data sources than SQL Server?
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
- Proposed as answer by Naomi N Sunday, August 16, 2020 4:20 AM
Friday, August 14, 2020 9:19 PM