Reputation: 333
I got this error:
The connection was not closed. The connection's current state is open.
when trying to save my data from application. I'm using VB.net and sql server 2008.
this is my code:
Public Function Insert(ByVal Code As String, ByVal Name As String, Optional ByVal trans As SqlTransaction = Nothing) As Boolean
Dim iResult As Integer
Dim arrColumn() As String = {"Code", "Name"}
Dim arrValue() As Object = {Code, Name}
oConn.Open()
Dim SQLString As String = GenInsert("BNK", arrColumn, arrValue)
Dim comm As New SqlCommand
Try
'iResult = SCommand.Execute(SQLString, oConn)
If trans IsNot Nothing Then
oConn = trans.Connection
comm.Transaction = trans
Else
oConn.Open()
End If
comm.Connection = oConn
comm.CommandText = SQLString
iResult = comm.ExecuteNonQuery
Catch ex As Exception
Throw ex 'the error is pointed here
Finally
If trans Is Nothing Then
oConn.Close()
End If
End Try
am I missing or something here?
-thanks for your help
Upvotes: 1
Views: 1890
Reputation: 228
If oConn
state is closed then only open the connection.
Instead of oConn.Open()
use if (oConn.State==ConnectionState.Close) then oConn.Open()
Upvotes: 1
Reputation: 1708
You are opening the connection twice. Please remove the first oConn.Open()
line, just above the SQLString
declaration.
Upvotes: 1