wonder_gal
Active member
- Joined
- Jun 5, 2006
- Messages
- 34
- Programming Experience
- 1-3
I have coded a main program having the database connection string being defined, and the main program calling other sub programs using the database connection defined in main program.
I got this error message here.
"System.InvalidOperationException. There is already an open DataReader associated with this Connection which must be closed first. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing) ..."
Anyone can help?
I got this error message here.
"System.InvalidOperationException. There is already an open DataReader associated with this Connection which must be closed first. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing) ..."
VB.NET:
Public Sub Main()
Dim oConn As SqlClient.SqlConnection
Dim oCommand_ValidBroadcast As SqlClient.SqlCommand
Dim oReaderValidBroadcast As SqlClient.SqlDataReader
[B]oConn[/B] = GetConnection_Broadcast()
.........................
strSQL = "Select BCT_Group.GP_Type, BCT_Group_Contact.Mobile_No from BCT_Group, BCT_Job_Group, BCT_Group_Contact where BCT_Job_Group.Job_ID = " & Job_ID & " and BCT_Group.GP_ID = BCT_Job_Group.GP_ID and BCT_Group.GP_ID = BCT_Group_Contact.GP_ID and BCT_Group.GP_Type <> 'Blocked'"
oCommand_ValidBroadcast = oconn.CreateCommand
oCommand_ValidBroadcast.CommandText = strSQL
oCommand_ValidBroadcast.CommandType = CommandType.Text
oReaderValidBroadcast = oCommand_ValidBroadcast.ExecuteReader
Do While oReaderValidBroadcast.Read
Mobile_Number = oReaderValidBroadcast("Mobile_No")
Group_Type = oReaderValidBroadcast("GP_Type")
If FunctionA(Mobile_Number, BP_ID, Cust_ID, Job_ID, Message, smsCharge, Message_Type, [B]oConn[/B]) Then
............
Else
............
End If
Loop
oReaderValidBroadcast.Close()
oconn.Close()
End Sub
Private Function FunctionA(ByVal p_szMobile_No As String, _
ByVal p_szBP_ID As Integer, _
ByVal p_szCust_ID As Integer, _
ByVal p_szJob_ID As Integer, _
ByVal p_szMessage As String, _
ByVal p_szCharge As Integer, _
ByVal p_szMessage_Type As String, ByVal oConn As SqlClient.SqlConnection) As Boolean
.......................
Try
FunctionB(msgId, Job_ID, p_szMobile_No, p_szMessage, p_szCharge, BP_GW_Shortcode, BP_ID, [B]oConn[/B])
Catch Err As Exception
..................
End Try
End Function
Private Function FunctionB(ByVal p_szMsgId As String, _
ByVal p_szJob_ID As Integer, _
ByVal p_szMobile_No As String, _
ByVal p_szMessage As String, _
ByVal p_szCharge As Integer, _
ByVal p_szBP_GW_Shortcode As String, _
ByVal p_szBP_ID As Integer, ByVal oConn As SqlClient.SqlConnection)
Dim oCommand_Insert As SqlClient.SqlCommand
Dim strSQL_Insert As String
Try
strSQL_Insert = "INSERT INTO BCT_Broadcast_Message_History(" _
& "MsgId," _
& "Job_ID," _
& "Mobile_No," _
& "Message," _
& "Charge," _
& "ShortCode," _
& "BP_ID)" _
& " VALUES('" _
& p_szMsgId & "'," _
& p_szJob_ID & ",'" _
& p_szMobile_No & "','" _
& Replace(p_szMessage, "'", "''") & "'," _
& p_szCharge & ",'" _
& p_szBP_GW_Shortcode & "'," _
& p_szBP_ID & ")"
oCommand_Insert = [B]oConn[/B].CreateCommand
oCommand_Insert.CommandText = strSQL_Insert
oCommand_Insert.CommandType = CommandType.Text
oCommand_Insert.ExecuteNonQuery() 'the error occurs at this line
Catch err As Exception
.............
Finally
oCommand_Insert.Dispose()
oCommand_Insert = Nothing
End Try
End Function
Anyone can help?