"Attempting to read while the reader is closed has no effect."
P粉884667022
P粉884667022 2024-03-31 14:58:58
0
1
273

I'm trying to write data from mysql uinlg c# to a .txt file but I'm getting the error "Invalid read attempt while reader is closed.".

Code here:

StreamWriter sr_Produto = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\produto.txt", true);
            bdcon.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT nome from produtos",bdcon);
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                sr_Produto.Write(dr["nome"].ToString());
            }
            bdcon.Close();
            bdcon.Open();
            StreamWriter sr_Quantidade = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\quantidade.txt", true);
            MySqlCommand cmd1 = new MySqlCommand("SELECT quantidade from produtos_pedidos",bdcon);
            MySqlDataReader dr1 = cmd1.ExecuteReader();
            if (dr.Read())
            {
                sr_Produto.Write(dr["quantidade"].ToString());
            }
            bdcon.Close();
            bdcon.Open();
            StreamWriter sr_Subtotal = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\subtotal.txt", true);
            MySqlCommand cmd2 = new MySqlCommand("SELECT subtotal from produtos_pedidos", bdcon);
            MySqlDataReader dr2 = cmd2.ExecuteReader();
            if (dr.Read())
            {
                sr_Produto.Write(dr["subtotal"].ToString());
            }
            bdcon.Close();
            bdcon.Open();
            StreamWriter sr_Valor = new StreamWriter(@"C:\Enzo\PAP\PAP\Ficheiros\valor.txt", true);
            MySqlCommand cmd3 = new MySqlCommand("SELECT valor from produtos");
            MySqlDataReader dr3 = cmd3.ExecuteReader();
            if (dr.Read())
            {
                sr_Produto.Write(dr["valor"].ToString());
            }
            bdcon.Close();

I would be happy if anyone could help me.

P粉884667022
P粉884667022

reply all(1)
P粉121081658

If you look at the code, you are creating MySqlDataReader dr and loading it, then closing it and creating and loading MySqlDataReader dr1. Then you try to read the closed dr . It also looks like you may be trying to write to the wrong StreamWriter.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!