I'm receiving an "A DataReader associated with this Command is already open. It must first be closed" error (translated from French, might not be accurate). I understand the problem but have not a trivial way to know which the “already open DataReader” is. My question is: When I stop on the error, do I have a way to list or see the currently opened dataReders.
Thanks in advance.
BB
Probably you are using the same connection. Try to enclose your code in a using statement as showed below
using (SqlConnection conn = new SqlConnection())
{
conn.Open();
Sqlmd.Connection = conn; // Sqlmd is your command
SqlDataAdapter da = new SqlDataAdapter(Sqlmd);
//...etc
}
Related
Hi all I'm having trouble with regards to opening my connection on my mysql database on xampp. I'm not quite sure about the issue here. But please do correct me if I'm wrong, is there a problem with my connectionstring script or does Visual Studio 2019 handle it differently now compared to it's previous versions. I've tried removing the "con.Open()" line. but the problem with this is I can't seem to even be able to insert some data into my databse without the "con.Open()" line
here's the code by the way.
Module Module1
Public con As New MySqlConnection
Public cmd As New MySqlCommand
Sub openCon()
con.ConnectionString = "server=localhost; username=admin; password=; database=db_tutorial"
con.Open()
End Sub
End Module
here's an image of the error by the way.
How do I change connection and execute some query in my form load during run time? it's more like opening multiple connections simultaneously but how do I do that?
Example of the Database info:
EDIT: I do have the Query but I don't know how to open the other connection during run time. the structure of their database is the same so there's no problem when executing the query. The problem is just how do I change the connection during runtime without pressing anything.
Based on #jmcilhinney answer in an external forum. The adapter is using one connection for the Select and another connection for the Insert. The trick is setting the .AcceptChangesDuringFill to False The default changes the Added to Unchanged.
Private Sub UpdateADifferentDatabase()
Using cnSource As New MySqlConnection("First connection string"), cnDestination As New MySqlConnection("Second connection string")
Using selectCommand As New MySqlCommand("Select statement here", cnSource), insertCommand As New MySqlCommand("Insert statement here ", cnDestination)
Using da As New MySqlDataAdapter()
da.SelectCommand = selectCommand
da.InsertCommand = insertCommand
'The following allows the .DataRowState to remain as Added (normally it is changed to Unchanged by the .Fill method)
da.AcceptChangesDuringFill = False
Dim dt As New DataTable
da.Fill(dt)
da.Update(dt)
End Using
End Using
End Using
End Sub
Firstly, as far as I know sharing the username/password should NOT be done as I'm attempting to make a user register/login program. My server is remote, so I connect to it with an IP address. I have been trying for a very long time but I always run into errors.
Even when providing the MySQL Username and password, it fails entirely.
Some stuff I've tried:
Dim connection As New MySqlConnection("datasource=(ip);port=22;username=root;password=(pwd);database=(db)")
Dim connection As New MySqlConnection("datasource=(ip);port=3306;username=root;password=(pwd);database=(db)")
Dim connection As New MySqlConnection("server=(ip);port=22;username=root;password=(pwd);database=(db)")
And then I realised another thing about my server, I require the SSH option when connecting so I tried this. However I still encounter the same errors on opening the MySQLConnection (portFwld.Start & Client.Connect is successful)
Dim ConnectionInfo = New PasswordConnectionInfo("(IP)", "root", "(PWD)")
Dim ConString = New MySqlConnectionStringBuilder
ConnectionInfo.Timeout = TimeSpan.FromSeconds(7)
Dim client = New SshClient(ConnectionInfo)
client.Connect()
Dim portFwld = New ForwardedPortLocal("127.0.0.1", Convert.ToUInt32("3306"), "(IP)", Convert.ToUInt32("22"))
client.AddForwardedPort(portFwld)
portFwld.Start()
ConString.Server = "(IP)"
ConString.Port = "22"
ConString.UserID = "root"
ConString.Password = "(PWD)"
ConString.Database = "(DB)"
Dim Connection = New MySqlConnection(ConString.ToString)
Connection.Open()
The errors I encountered were mostly these:
{"Reading from the stream has failed."}
and:
{"Unable to connect to any of the specified MySQL hosts."}
I literally have no idea why nothing is working whatsoever. I've searched and searched and I cannot find the solution. Also a reminder; I'm pretty sure I don't want the password revealed so blatantly in a string, let alone revealed at all.
Please be considerate as I'm completely new to this and just want to setup a community program just like any other program/site (like accounts here on Stackoverflow).
Thanks in advance.
The issue was that I needed to go to my server's cPanel and go to the allowed IPs area and add %, after that connection was all successful.
My boyfriend and I have spent the last 2 hours trying to get this to work, we have tried putting all the data connections into try's, changing the the connections to gloabal's but whatever we do we end up back at the same problem. The page will load fine when loaded via localhost, but when we try to get in via debug it hits this error.
If we put the code that fails into a try it goes through each connection and then will load, but each part will have been caught in the catch.
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code
There is already an open DataReader associated with this Connection which must be closed first.
Here's the code, each sub or function that uses MySQL connection has a close in the same as this function. We are out of ideas to what it causing it, as only does it we try and enter debug in Visual Studio.
Public Function getID()
Dim cmd As New MySqlCommand()
Dim reader As MySqlDataReader
Dim rtr As Integer
cmd.CommandText = "MySQLSTATEMENT"
cmd.Connection = CON_STRING
Connection.Open()
reader = cmd.ExecuteReader
If (reader.HasRows()) Then
reader.Read()
rowID = reader.Item(0)
End If
Connection.Close()
reader.Close()
cmd.Dispose()
End Function
You Should Close reader first and then you can close the connection.
I would recommend Using Statement for connection that will take care of closings the connection.
I want to create a Dot net application and provide an environment to a user may be Multiline Text box, where user can Paste the predefined SP and Execute. After execution this Sp should be created in DB
Any ideas are invited..
I assume you want to do thid for a internal support application or something like that, not to the end-user, right?
Anyway, you need to be more specific, but the way you would create a procedure doesnt differ the way you would run a insert statment for example.
Simple example:
SqlConnection objConnection = new SqlConnection(your_connection_string);
SqlCommand objCommand = new SqlCommand(tbProcedureCode.text, objConnection);
objCommand.CommandType = CommandType.Text;
objConnection.Open();
objCommand.ExecuteNonQuery();