How to avoid Error "There is already an open DataReader associated with this Command" in MSSql 2000 - sql-server-2000

I am using SqlBulkCopy to copy data to 2 separate table in same database.
obj_Command.CommandText = "Select * from tmpInvDtlMast where InvNo='111'"
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "tmpInvDtlMast"
Try
bulkCopy.WriteToServer(obj_Command.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "InvDtlMast"
Try
bulkCopy.WriteToServer(obj_Command.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using
In the second Bulk upload this error occurs
There is already an open DataReader associated with this Command
I have tried "MultipleActiveResultSets=true" with my connection string
con1.ConnectionString = "Data Source=" & SqlServer & ";Initial Catalog=" & Database & ";Persist Security Info=True;User ID=" & User & ";Password=" & Pass & ";MultipleActiveResultSets=true"
After googling more solutions I got to know that MultipleActiveResultSets=true not support for MSSql 2000. Is there anything to do to solve this in Sql2000. Please help
Im using vb.net 2008

As a workaround try duplicating connection and command:
obj_Command.CommandText = "Select * from tmpInvDtlMast where InvNo='111'"
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "tmpInvDtlMast"
Try
bulkCopy.WriteToServer(obj_Command.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(**con2**)
bulkCopy.DestinationTableName = "InvDtlMast"
Try
bulkCopy.WriteToServer(**obj_Command2**.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using

I have solved my problem myslf. What I did is remove
obj_Command.ExecuteReader()
with new SqlDataReader instance.
obj_Command.CommandText = "Select * from tmpInvDtlMast where InvNo='111'"
Dim dr as SqlDataReader()
dr=obj_Command.ExecuteNoneQuery()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "tmpInvDtlMast"
Try
bulkCopy.WriteToServer(dr)
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
dr.Close()
End Try
End Using
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "InvDtlMast"
Try
bulkCopy.WriteToServer(dr)
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
dr.Close()
End Try
End Using

Related

vb.net cannot update in oracle DB

I am developing small program with VB.NET and Oracle DB.
There is a problem when I want to update the column value.
This is my code:
getOraConn()
trans = ora_conn.BeginTransaction(IsolationLevel.ReadCommitted)
cmdOr.Transaction = trans
Try
cmdOr.CommandText = "update tbl_study set flag='0'"
cmdOr.Connection = ora_conn
cmdOr.ExecuteNonQuery()
trans.Commit()
ora_conn.Close()
MessageBox.Show("sucess")
Catch ex As Exception
MessageBox.Show(ex.Message)
trans.Rollback()
End Try
When I running this code, it is always hangs at 'cmdOr.ExecuteNonQuery()' and then the column in DB still not change.
Here is my Oracle connection string:
Function getOraConn() As OracleConnection
Dim oradb As String = "Data Source=SPEC; User Id=sta; Password=sta123;Pooling=False;"
Try
ora_conn = New OracleConnection(oradb)
ora_conn.Open()
Catch ex As Exception
MsgBox("connection failed", MsgBoxStyle.Critical)
End Try
End Function

vb.net how to show data from sql in lable

i want show my select syntac into lable but my code no work this only show 1 actualy data is 11
Try
If IsNothing(ds.Tables("kkpsurabaya")) = False Then
ds.Tables("kkpsurabaya").Rows.Clear()
End If
query = "SELECT count(total_telat) FROM kkpsurabaya WHERE LATE <=30 And Late >=1"
da = New SqlDataAdapter(query, conn)
da.Fill(ds, "kkpsurabaya")
Label7.Text = da.Fill(ds, "kkpsurabaya")
da.Dispose()
conn.Close()
Catch ex As Exception
FatalErrorOccured(ex)
End Try
You should not be using a data adapter and DataTable for this. That's for getting a tabular result set. If you want a single value then use a command and call ExecuteScalar.
Dim command As New SqlCommand(query, conn)
Label7.Text = command.ExecuteScalar().ToString()
To learn what ADO.NET objects to use in what situations, check out my examples here.
solved now
conn.Open()
query = "SELECT count(total_telat) as total_telat FROM kkpsurabaya WHERE total_telat <=30 and total_telat >=1"
cmd = New SqlCommand(query, conn)
Try
RD = cmd.ExecuteReader()
If RD.Read() Then
Label7.Text = RD.GetValue(0)
End If
RD.Close()
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
conn.Close()

Oracle transaction Rollback doesn't work

I created an Oracle transaction trying to save some data in two tables and I caused an error at the second procedure to call the rollback and check if it works.
The first procedure creates a job in USER_SCHEDULER_JOBS using the sys.dbms_scheduler.create_job.
Private Function CreateJobDef() As Integer
Try
Cursor.Current = Cursors.WaitCursor
conn = New OracleConnection
conn.ConnectionString = gApp.ConnectString
conn.Open()
Dim cmd As OracleCommand = conn.CreateCommand()
Dim oraclTrans As OracleTransaction
oraclTrans = conn.BeginTransaction()
cmd.Transaction = oraclTrans
Try
cmd.CommandText = "TEST.CREATE_JOB_SCHEDULE"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Clear()
OracleCommandBuilder.DeriveParameters(cmd)
cmd.Parameters("in_job_name").Value = txtName.Text
cmd.Parameters("in_schedule_name").Value = cboSchedule.SelectedValue
cmd.Parameters("in_enabled").Value = If(chkEnabled.Checked, 1, 0)
cmd.Parameters("in_comments").Value = txtComments.Text
cmd.ExecuteNonQuery()
'Everything is OK
'-----------------------------------------------
cmd.CommandText = "TEST.Update_Job_Def"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Clear()
OracleCommandBuilder.DeriveParameters(cmd)
cmd.Parameters("in_job_id").Value = "WQwqwq" 'I set this to a string to cause an error in the second procedure to check the rollback
cmd.Parameters("in_job_name").Value = txtName.Text
cmd.Parameters("in_job_type").Value = cboType.SelectedItem
cmd.Parameters("in_report_nav_id").Value = cboReport.SelectedValue
cmd.Parameters("in_mail_address_to").Value = txtMailTo.Text
cmd.ExecuteNonQuery()
oraclTrans.Commit()
Catch ex As OracleException
Msgbox(ex.ToString)
oraclTrans.Rollback()
Catch ex As Exception
Msgbox(ex.ToString)
oraclTrans.Rollback()
Finally
conn.Close()
If conn IsNot Nothing Then conn.Dispose()
If cmd IsNot Nothing Then cmd.Dispose()
End Try
Catch ex As OracleException
Msgbox(ex.ToString)
Catch ex As Exception
Msgbox(ex.ToString)
Finally
Cursor.Current = Cursors.Default
End Try
End Function
The problem is that the first procedure is create a new Job, although I have caused an error at the second procedure and the rollback is called.
Any ideas?
Thanks
There is an implicit commit in a DDL statement and in many Oracle procedure calls that create or modify objects such as a scheduled job. Therefore you cannot rollback a job creation procedure.

No data available after an Insert operation VB.NET - Windows Forms and Web Service

VB.NET and ASMX Web Service. I am still having the same trouble of getting no data after an insert operation.
Here is my insert code:
Try
'create a MySqlCommand to represent the query
If sqlConn.State = ConnectionState.Closed Then
sqlConn = New MySqlConnection(conStr)
sqlConn.Open()
End If
Dim myCommand As MySqlCommand = New MySqlCommand(strQuery, sqlConn)
If myCommand.ExecuteNonQuery() <> 0 Then
Result = True
End If
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 2 " & ex.Message)
Finally
sqlConn.Close()
sqlConn.Dispose()
End Try
The strQuery will be a simple insert statement.
The code for retrieving data as a dataset is as follows
Public Function ExecuteQuery(ByVal strQuery As String) As DataSet
Dim ds As DataSet = New DataSet 'create a DataSet to hold the results
Try
'create a MySqlCommand to represent the query
If sqlConn.State = ConnectionState.Closed Then
sqlConn = New MySqlConnection(conStr)
sqlConn.Open()
End If
Dim sqlcmd As MySqlCommand = sqlConn.CreateCommand()
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = strQuery
'create a MySqlDataAdapter object
Dim sqlDa As MySqlDataAdapter = New MySqlDataAdapter
sqlDa.SelectCommand = sqlcmd
Try
'fill the DataSet using the DataAdapter
sqlDa.Fill(ds, "Results")
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 1 " & ex.Message)
End Try
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 2 " & ex.Message)
Finally
sqlConn.Close()
sqlConn.Dispose()
End Try
Return ds
End Function
I am Inserting data from one thread and retrieving data from another (not the same instance of connection is used).
This works perfect if I retrieve data after a 15 minutes gap.
But I want to get the result at once.
Please help.
I would change the method as below
Public Function ExecuteQuery(strQuery As String) As DataSet
Dim ds As New DataSet()
Try
Using con = New MySqlConnection(conStr) 'using statements...
Using cmd = New MySqlCommand(strQuery, con) 'using statements...
Using sqlDa = New MySqlDataAdapter(cmd) 'using statements...
sqlDa.Fill(ds, "Results")
End Using
End Using
End Using
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 1 " + ex.Message)
End Try
Return ds
End Function
Public Function ExecuteNonQuery(strQuery As String) As Boolean
Try
Using con = New MySqlConnection(conStr) 'using statements...
Using cmd = New MySqlCommand(strQuery, con) 'using statements...
con.Open()
If cmd.ExecuteNonQuery() > 0 Then ' I have change the condition
Return True
End If
End Using
End Using
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 2 " + ex.Message)
End Try
Return False
End Function

Fill data grid view from sql table

I have four columns in Datagridview. I want to fill first two columns with data from sql database. I try to fill Datagridview. It not display data, but it generate rows.
This is my code:
getConnect()
Try
Conn.Open()
Dim strSQL As String = "SELECT EMP_ID, EMP_NAME FROM EMPLOYEE ORDER BY EMP_NAME ASC"
Conn.Close()
Dim da As New SqlDataAdapter(strSQL, Conn)
Dim dt As New DataTable("EMPLOYEE")
da.Fill(dt)
ATCGRID.DataSource = dt
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
Please check my code and give me solution...
Try this code .
getConnect()
Try
Conn.Open()
Dim strSQL As String = "SELECT EMP_ID, EMP_NAME FROM EMPLOYEE ORDER BY EMP_NAME ASC"
Conn.Close()
Dim da As New SqlDataAdapter(strSQL, Conn)
Dim ds As new Dataset
da.Fill(ds,"EMPLOYEE")
ATCGRID.DataSource = ds.tables(0)
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
Public Sub OpenConnect()
Try
CmdSql.Connection = conn
conn.Open()
CmdSql.CommandType = CommandType.Text
Catch ex As Exception
' MsgBox(ex.Message)
End Try
End Sub
' this worked perfectly
Thanks for the sub getConnect() it worked perfectly. mine also worked.
Sub RefreshGrid()
' refresh the datagrid
OpenConnect()
CmdSql.CommandText = "SELECT manager_id,manager_name FROM tbl_Manager"
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
dgvMgr.DataSource = ds.Tables(0)
'THIS MODULE WORKED JUST Please Fill Property Columns
'DataPropertyName as Field Database,
'Eg : Column1-DataPropertyName=manager_id and so on.
End Sub