C
C#13mo ago
Alix

error about datareader

When trying to insert some fake clients into my database i am getting this error Error DBConnectThere is already an open DataReader associated with this Connection which must be closed first. when i search for this error i get redirect to the sdk code. So does this mean that there is a bug in the sdk or am i doing something wrong??
c#
private void AddFakeClient()
{
string clientName = GenerateRandomClientName();
string clientEmail = GenerateRandomClientEmail();
string clientCompany = GenerateRandomClientCompany();
Debug.WriteLine("opening create db");

using (DBConnect db = new DBConnect())
{
MySqlDataReader userRow = null;

try
{
db.Open();
Debug.WriteLine("opening create query");
string selectQuery = "SELECT id, username FROM users";
userRow = db.ExecuteReader(selectQuery);

var Session = Application.Current.Resources;

if (Session["username"] != null && Session["id"] != null)
{
username = Session["username"].ToString();
foreignid = Session["id"].ToString();
}
else
{
Debug.WriteLine("Session username or id is null.");
return;
}

if (userRow.HasRows)
{
while (userRow.Read())
{
string userId = userRow["id"].ToString();
string userName = userRow["username"].ToString();
string insertQuery = "INSERT INTO clients (Client, email, Company, foreignid) VALUES ('" +
clientName + "', '" + clientEmail +
"', '" + clientCompany + "', '" + foreignid + "')";
db.ExecuteNonQuery(insertQuery);
}
}
else
{
DisplayAlert("Error", "Account not detectable.", "OK");
}
}
catch (Exception ex)
{
DisplayAlert("Error", $"{ ex.Message }", "OK");
}
finally
{
Debug.WriteLine("closing create query");
userRow?.Close();
Debug.WriteLine("closing create db");
db.Close();
}
}
}
c#
private void AddFakeClient()
{
string clientName = GenerateRandomClientName();
string clientEmail = GenerateRandomClientEmail();
string clientCompany = GenerateRandomClientCompany();
Debug.WriteLine("opening create db");

using (DBConnect db = new DBConnect())
{
MySqlDataReader userRow = null;

try
{
db.Open();
Debug.WriteLine("opening create query");
string selectQuery = "SELECT id, username FROM users";
userRow = db.ExecuteReader(selectQuery);

var Session = Application.Current.Resources;

if (Session["username"] != null && Session["id"] != null)
{
username = Session["username"].ToString();
foreignid = Session["id"].ToString();
}
else
{
Debug.WriteLine("Session username or id is null.");
return;
}

if (userRow.HasRows)
{
while (userRow.Read())
{
string userId = userRow["id"].ToString();
string userName = userRow["username"].ToString();
string insertQuery = "INSERT INTO clients (Client, email, Company, foreignid) VALUES ('" +
clientName + "', '" + clientEmail +
"', '" + clientCompany + "', '" + foreignid + "')";
db.ExecuteNonQuery(insertQuery);
}
}
else
{
DisplayAlert("Error", "Account not detectable.", "OK");
}
}
catch (Exception ex)
{
DisplayAlert("Error", $"{ ex.Message }", "OK");
}
finally
{
Debug.WriteLine("closing create query");
userRow?.Close();
Debug.WriteLine("closing create db");
db.Close();
}
}
}
`
8 Replies
canton7
canton713mo ago
Which line throws the exception? What is DBConnect?
Alix
AlixOP13mo ago
c#
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Diagnostics;

namespace MauiInvoiceApplication.Classes
{
class DBConnect : IDisposable
{
MySqlConnection conn;
string myConnectionString;
static string host = "phpmyadmin.adonkers.com";
static string database = "InvoiceApp";
static string userDB = "";
static string password = "";
static string ssltype = "None";
static string dbport = "3306";
static string dbpubkeyre = "true";
static string Multipleact = "true";
static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password + ";SslMode=" + ssltype + ";Port=" + dbport + "AllowPublicKeyRetrieval=" + dbpubkeyre + "MultipleActiveResultSets=" + Multipleact;

public bool Open()
{
strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password +
";SslMode=" + ssltype + ";Port=" + dbport + ";AllowPublicKeyRetrieval=" + dbpubkeyre;
conn = new MySqlConnection(strProvider);
conn.Open();
return true;
}

public void Close()
{
conn.Close();
conn.Dispose();
}

public DataSet ExecuteDataSet(string sql)
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.Fill(ds, "result");
return ds;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return null;
}

public MySqlDataReader ExecuteReader(string sql)
{
try
{
MySqlDataReader reader;
MySqlCommand cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
return reader;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return null;
}

public int ExecuteNonQuery(string sql)
{
try
{
int affected;
MySqlTransaction mytransaction = conn.BeginTransaction();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
affected = cmd.ExecuteNonQuery();
mytransaction.Commit();
return affected;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return -1;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (conn != null)
{
conn.Dispose();
conn = null;
}
}
}
}
}
c#
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Diagnostics;

namespace MauiInvoiceApplication.Classes
{
class DBConnect : IDisposable
{
MySqlConnection conn;
string myConnectionString;
static string host = "phpmyadmin.adonkers.com";
static string database = "InvoiceApp";
static string userDB = "";
static string password = "";
static string ssltype = "None";
static string dbport = "3306";
static string dbpubkeyre = "true";
static string Multipleact = "true";
static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password + ";SslMode=" + ssltype + ";Port=" + dbport + "AllowPublicKeyRetrieval=" + dbpubkeyre + "MultipleActiveResultSets=" + Multipleact;

public bool Open()
{
strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password +
";SslMode=" + ssltype + ";Port=" + dbport + ";AllowPublicKeyRetrieval=" + dbpubkeyre;
conn = new MySqlConnection(strProvider);
conn.Open();
return true;
}

public void Close()
{
conn.Close();
conn.Dispose();
}

public DataSet ExecuteDataSet(string sql)
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.Fill(ds, "result");
return ds;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return null;
}

public MySqlDataReader ExecuteReader(string sql)
{
try
{
MySqlDataReader reader;
MySqlCommand cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
return reader;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return null;
}

public int ExecuteNonQuery(string sql)
{
try
{
int affected;
MySqlTransaction mytransaction = conn.BeginTransaction();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
affected = cmd.ExecuteNonQuery();
mytransaction.Commit();
return affected;
}
catch (Exception ex)
{
Debug.WriteLine("Error DBConnect" + ex.Message);
}
return -1;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (conn != null)
{
conn.Dispose();
conn = null;
}
}
}
}
}
that is my db connect and i am not getting any line
SG97
SG9713mo ago
can you post a screenshot of the error
Alix
AlixOP13mo ago
No description
Alix
AlixOP13mo ago
got nothing more sadly
canton7
canton713mo ago
You're executing a query while there's an open DataReader? https://stackoverflow.com/a/7404092/1086121
Stack Overflow
Exception: There is already an open DataReader associated with this...
I have below code and I am getting exception: There is already an open DataReader associated with this Connection which must be closed first. I am using Visual Studio 2010/.Net 4.0 and MySQL fo...
canton7
canton713mo ago
(top google hit for "there is already an open datareader associated with this connection")
Alix
AlixOP13mo ago
yeah i know that but i do close them everywhere correctly right i am using row.close() right so it should close the datareader right? i found out what the problem was the code didn't like that i had 2 dbconnect with the same name (db) so i renamed one and it is fixed now
Want results from more Discord servers?
Add your server