ERROR [HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt in VB.net - vb.net-2010

Hello I have problem with vb.net, when I click that button, it always show error
ERROR [HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy
with results for another hstmt
Anyone can help me?
Private Sub cmbposting_Click(sender As Object, e As EventArgs) Handles cmbposting.Click
cmd = New OdbcCommand("select * from stok_master where stok_cd='" & txtstok_cd.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
Dim kurangistok As String = "Update stok_master set " & _
"qty='" & rd.Item(6) - Val(txtreqqty.Text) & "' " & _
"where stok_cd='" & txtstok_cd.Text & "'"
cmd = New OdbcCommand(kurangistok, conn)
**cmd.ExecuteNonQuery()**
End If
End Sub

As per Microsoft
When accessing an SQL Server database as an ODBC data source in Visual
Basic versions 3.0 or 4.0 with the Microsoft Access version 2.0
Compatibility Layer installed, error message 3146 may occur:
ODBC-call failed. [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt [#0]
This error is the result of the SQL Server ODBC driver. The driver can
only handle one active statement at a time. The statement remains
active until all the rows are fetched.
You can possibly complete reading all the rows, load it to a DataTable and then iterate through it to update the table again.
Something like
var dataReader = cmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(dataReader);
And then iterate through the dataTable like
foreach(DataRow row in dataTable.Rows)
{
.......
}

Related

SQL query table name syntax error through ODBC connnection using VB.Net

I have an SQL query that I'm trying to write to obtain information from an Access table. I'm using Visual Studio 2019 and writing in VB.Net. I have had no issue establishing my ODBC connection, but my SQL string is giving me an error
System.Data.Odbc.OdbcException: 'ERROR [HY000] [DataDirect][ODBC Progress OpenEdge Wire Protocol driver][OPENEDGE]Syntax error in SQL statement at or about "[item-desc] FROM PUB.item" (10713)'
Right now I'm just trying to grab a piece of information from a specific column, but if I use a wildcard as my SELECT the query works. The Access table has a column named item-desc that I'm trying to grab from, but I also tried one called key just to see if it was an issue with the - character. I've tried different formatting with the brackets, quotes adding table names, etc but I have gotten the same error each time. Any insight would be appreciated; I have been unable to find any further information regarding this specific circumstance. Thanks!
Public Sub MySQLConnect() ' Open ODBC Connection for SQL query
Dim Conn As OdbcConnection
Conn = New OdbcConnection("DRIVER = Progress OpenEdge 12.1 Driver;PWD=XXXXXXX;KeepAlive=0;LoadBalanceTimeout=0;FailoverPreconnect=0;FailoverGranularity=0;FailoverMode=0;ValidateServerCertificate=1;EncryptionMethod=0;ConnectionRetryDelay=3;ConnectionRetryCount=0;LoadBalancing=0;DefaultLongDataBuffLen=2048;QueryTimeout=0;LoginTimeout=15;ArraySize=50;DefaultIsolationLevel=1;EnableTimestampWithTimezone=1;UseWideCharacterTypes=0;StaticCursorLongColBuffLen=4096;LogonID=TESTING;Database=demo;PortNumber=5555;HostName=192.168.000.000;Description=Encompix Demo;WorkArounds2=8192;FILEDSN=C:\Program Files\Common Files\ODBC\Data Sources\EncompixDemo.dsn")
Dim SQLString As String = "SELECT [item-desc] FROM PUB.item" 'WHERE [key] = 196" ' Add table name, search parameters, CADLink variable
Dim cmd As OdbcCommand = New OdbcCommand(SQLString, Conn)
Dim Reader As OdbcDataReader
Dim ColumnCount As Integer
Dim output As String
Dim data As Object() = New Object(10) {}
Conn.Open()
MsgBox("Connected, baby!")
Reader = cmd.ExecuteReader()
While Reader.Read()
ColumnCount = Reader.GetValues(data)
output = ""
For i As Integer = 0 To ColumnCount - 1
output = output & " " & data(i)
Debug.WriteLine(data(i))
Next
'Debug.WriteLine(output)
End While
Conn.Close()
End Sub```

OleDb exception System.Data.OleDb.OleDbException (0x80004005): System resource exceeded

VB2013: I am getting the exception System.Data.OleDb.OleDbException (0x80004005): System resource exceeded. When I query an MS Access database.
Here is what I do in my code:
'Make the connection to connDB
Public connDB As OleDbConnection
connDB = New OleDbConnection
With connDB
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DbFile & ";Persist Security Info=True;Jet OLEDB:Database Password=xxxxxx;"
.Open()
End With
'iterate through some 2500 obects. each object has a set of codes and we will get their description here
GetSysDefinitions (list of codes for an object)
'Close the connection to connDB;
Public Function GetSysDefinitions(sysCodes As List(Of String)) As String
Try
'check to see if the db is available
If connDB Is Nothing Then Return ""
'set up the SQL to get the System Codes and Definitions
Dim sCodes As String = "'" & String.Join("', '", sysCodes) & "'"
Dim sql As String = "SELECT * " & _
"FROM SYS_Codes " & _
"WHERE CODE IN(" & sCodes & ") ; "
Dim daLs As New OleDbDataAdapter(sql, connDB)
Dim dsLs As New DataSet
Dim dtLs As New DataTable
daLs.SelectCommand.CommandTimeout = 60 '60 seconds for the command to timeout
daLs.Fill(dsLs, "Sys") '<=== Exception here at some point
dtLs = dsLs.Tables(0)
'do something with records returned
Catch ex As Exception
Debug.Print(ex.ToString)
End Try
End Function
This all works great. At some point however I get the exception System.Data.OleDb.OleDbException (0x80004005): System resource exceeded at the line daLs.Fill. I just am not sure why resources are being exceeded and what I need to do to avoid this exception. Seems like I make one connection and then use it to make many queries. Should work right?
Thanks Çöđěxěŕ and Andrew Morton. Dont remeber where I got the code snippet but have been using it for years. I guess the difference is this time Im using that routine in a large number of calls. here is my updated code which I tested and no exceptions:
Using daLs As New OleDbDataAdapter(sql, connDb)
Using dtLs As New DataTable
'fill in the DataTable
daLs.Fill(dtLs)
dtLs.TableName = "CoreSys"
'check for how many rows were returned
'parse out rows
End Using
End Using

How to load data from oracle database using .dsn? (VB.NET)

I'm trying to test if the connection from my utility to oracle DB is working by using .dsn . I've searched from many forums but to no avail . Here is my code:
Dim filedsn As String = "C:\my_dsn.dsn"
Dim uid As String = "id123"
Dim pwd As String = "1234"
Dim cn As OdbcConnection
cn = New OdbcConnection("Driver=Oracle in OraClient11g_home2;Provider=msdaora;dsn=" & filedsn & ";uid=" & uid & ";pwd=" & pwd & ";")
Dim mystring As String = "Select * from DD_CADS1.PDTABLE_12_1001"
Dim cmd As OdbcCommand = New OdbcCommand(mystring)
cn.Open()
MsgBox("Connected")
cn.Close()
But these error appears..
ERROR [HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error
Can you tell me what's the problem with my codes? Is there any other way to connect to oracle database through .dsn using VB.net? I'm currently using ** Oracle 11g**.
The biggest problem in your code is that you use ODBC to connect to Oracle. You should use ODP.NET, which is even recommended by Microsoft. What you doing is there [most likely] for backwards compatibility and not to write the new stuff.
Here your problem is in connection string, which should look like this
"DSN=oracledsn;UID=myUID;PWD=myPWD;Integrated Security=no;"
And you don't need command just to test connection
using cn= New OdbcConnection(".....")
cn.Open()
if cn.State = ConnState.Open Then MessageBox.Show(...)
end using
When you use using it will take care of closing and disposing of connection.

Executing an SQL query in an Access Database in VB.NET

I have already posted one question about this piece of code before lol. But now I am having a different problem and need help with it. Basically this is code for a search button on a Windows Form coded in VB.NET.
Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
Dim cnnOLEDB As New OleDbConnection
Dim cmdOLEDB As New OleDbCommand
Dim strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\All Keyed Up, Lock & Safe, LLC.accdb"
cnnOLEDB.ConnectionString = strConnectionString
cnnOLEDB.Open()
Dim Connection As New SqlClient.SqlConnectionStringBuilder
Connection.DataSource = "file:///C:\Users\thelukester145\Documents\All%20Keyed%20Up\All%20Keyed%20Up,%20Lock%20&%20Safe,%20LLC.accdb"
Dim objSQLConnection = New SqlClient.SqlConnection(Connection.ConnectionString)
Dim cmd As New SqlCommand()
Dim myTable As New DataTable()
Dim SearchFor = SearchBox.Text
If AddressButton.Checked Then
cmd.Connection = objSQLConnection
cmd.CommandText = "SELECT * FROM [McDonald's-Corporate Stores] WHERE [Address] LIKE '%" & SearchFor & "%'"
Dim myAdapter As New SqlDataAdapter(cmd)
myAdapter.Fill(myTable)
DataGrid.DataGridView1.DataSource = myTable
ElseIf NumberButton.Checked Then
cmd.Connection = objSQLConnection
cmd.CommandText = "SELECT * FROM [McDonald's-Corporate Stores] WHERE [McOpCo#] LIKE '%" & SearchFor & "%'"
Dim myAdapter As New SqlDataAdapter(cmd)
myAdapter.Fill(myTable)
DataGrid.DataGridView1.DataSource = myTable
End If
DataGrid.Show()
cnnOLEDB.Close()
End Sub
The database that is attempting to be searched is a database of customers for our family buisness. I am trying to search by either the address or store number of our customer. I've worked through who knows how many fatal errors but the newest one has me sort of stumped. It is crashing on myAdapter.Fill(myTable). The error message it gives is this:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)
Any help would be much appreciated :)
Well, you are freely mixin OleDb classes with SqlClient classes. The two are for different database types. OleDb is needed for Access, SqlClient classes are used for Sql Server.
So you need to change all the classes SqlCommand, SqlCommandBuilder, SqlDataAdapter,SqlConnectionStringBuilder with the corresponding classes OleDbCommand, OleDbCommandBuilder, OleDbDataAdapter, OleDbConnectionStringBuilder
Infact, the error message is emitted by the SqlClient library that tries to use your connection string as it was a connectionstring for an SQL Server database and, of course, it fails to find it.
Also, I am not sure about that, but probably the DataSource property doesn't need the prefix file:/// and the Encoding of spaces with %20

"SELECT into statement" inside loop not working

I have a select into statement that works, but when i put it inside a Loop, it fails.
Does anyone have any idea why this is happening??
Thank you!
Here's the code:
For o = 0 To x
tables = ListBox1.Items.Item(o).ToString
sqlstr = "SELECT * into dbo." & tables & " FROM OPENROWSET('MSDASQL', 'Driver=Microsoft Visual FoxPro Driver; SourceDB=C:\exports\DBF; SourceType = DBF ', 'SELECT * FROM " & tables & "')"
mycmd = New SqlCommand
sqlcon = New SqlConnection(con)
mycmd.CommandText = sqlstr
sqlcon.Open()
mycmd.Connection = sqlcon
mycmd.ExecuteNonQuery()
sqlcon.Close()
Next
When i try to execute this it gives me following error
Cannot execute the query "SELECT * FROM AFED" against OLE DB provider "MSDASQL"
for linked server "(null)". OLE DB provider "MSDASQL"
for linked server "(null)" returned message
"[Microsoft][ODBC Visual FoxPro Driver]File 'afed.dbf' does not exist.".
Please check your foxpro db .. I think afed table does not exist in foxpro DB.and that is why you are not getting the error when put static value instead of o.