I did not close the previous DataReader, but where?

I changed my previous code, so I do not use "use". It works earlier, and the code in another class basically represents the same thing, but it works.

I have been looking at it for 2 hours, and I just canโ€™t understand where the problems may be.

I have only one reader, but every time I use the DisplayFileContent method, I get an error: Error: There is already an open DataReader associated with this command which must be closed first.

 // May be public so we can display // content of file from different forms. public void DisplayFileContent(string filePath) { // Counting all entries. int countEntries = 0; // Encrypting/Decrypting data. EncryptDecrypt security = new EncryptDecrypt(); using (OleDbConnection connection = new OleDbConnection()) { connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Persist Security Info=False;" + "Jet OLEDB:Database Password=" + hashPhrase.ShortHash(storedAuth.Password) + ";"; using (OleDbCommand command = new OleDbCommand ("Select * FROM PersonalData", connection)) { OleDbDataReader read; try { // Open database connection. connection.Open(); // Create a data reader. read = command.ExecuteReader(); // Clearing the textbox before proceeding. txtDisplay.Text = string.Empty; // Checking if there is any data in the file. if (read.HasRows) { // Reading information from the file. while (read.Read()) { // Count all entries read from the reader. countEntries++; // Reading all values from the file as string. // While each string is encrypted, we must decrypt them. // User name and password is the same as user provided // while authentication. txtDisplay.Text += "=== Entry ID: " + read.GetValue(0) + " ===" + Environment.NewLine; txtDisplay.Text += "Type: " + security.Decrypt (read.GetString(1), storedAuth.Password, storedAuth.UserName) + Environment.NewLine; if (!read.IsDBNull(2)) txtDisplay.Text += "URL: " + security.Decrypt(read.GetString(2), storedAuth.Password, storedAuth.UserName) + Environment.NewLine; if (!read.IsDBNull(3)) txtDisplay.Text += "Software Name: " + security.Decrypt(read.GetString(3), storedAuth.Password, storedAuth.UserName) + Environment.NewLine; if (!read.IsDBNull(4)) txtDisplay.Text += "Serial Code: " + security.Decrypt(read.GetString(4), storedAuth.Password, storedAuth.UserName) + Environment.NewLine; if (!read.IsDBNull(5)) txtDisplay.Text += "User Name: " + security.Decrypt(read.GetString(5), storedAuth.Password, storedAuth.UserName) + Environment.NewLine; if (!read.IsDBNull(6)) txtDisplay.Text += "Password: " + security.Decrypt(read.GetString(6), storedAuth.Password, storedAuth.UserName) + Environment.NewLine; txtDisplay.Text += Environment.NewLine; } } else { txtDisplay.Text = "There is nothing to display! " + "You must add something before so I can display anything here."; } // Displaying number of entries in the status bar. tsslStatus.Text = "A total of " + countEntries + " entries."; // Selecting 0 character to make sure text // isn't completly selected. txtDisplay.SelectionStart = 0; command.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } } } 
+4
source share
3 answers

You call command.ExecuteNonQuery immediately before the catch block. First you need to close your DataReader.

I would recommend wrapping code that uses the datareader in the using block anyway:

 using(OleDbDatareader read = command.ExecuteReader()) { ... } 

As indicated above, command.ExecuteNonQuery () is designed to execute commands from which you do not expect a return result. These are usually insertions, updates, or deletions, but may also include stored proc calls that do the same, or if you don't need the returned result

+6
source

In front of your line

 command.ExecuteNonQuery(); 

you need:

 read.Close(); 

In addition, it is important to know that using a connection does NOT automatically close it. So before the end of your connection using the instructions you will need

 connection.Close(); 
+3
source

I do not see the code to close the data reading.

Finally, add the catch part:

  finally { if (read != null) { read.Close(); } } 

EDIT # 1: I made a mistake: you must close it before executing the next command to either delete the last line (if necessary) before the catch block, or add a finally block or just add a read-use variable.

+1
source

Source: https://habr.com/ru/post/1384231/


All Articles