Buğrahan Öz
Buğrahan Öz

Reputation: 33

DataReader Associated with this Command Error

    OleDbConnection baglanti = 
        new OleDbConnection("connect");
    OleDbCommand komut = new OleDbCommand();
    OleDbDataReader dr;
    DataSet ds = new DataSet();
    OleDbDataAdapter da = new OleDbDataAdapter();
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if(textBox1.Text=="" || textBox2.Text=="" || textBox3.Text=="" || maskedTextBox1.Text=="" || dateTimePicker1.Text=="" || maskedTextBox2.Text=="" || textBox6.Text=="")
        {
            MessageBox.Show("Lütfen Boş alanları doldurunuz !!");
        }
        else
        {
            baglanti.Open();
            komut.Connection = baglanti;
            komut.CommandText=("Select TcKimlik from Hasta");
            dr = komut.ExecuteReader();
            while(dr.Read())
            {
                if(dr["TcKimlik"].ToString()==textBox1.Text.ToString())
                {
                    MessageBox.Show("aaaaa");
                }
                else
                {
                    komut.CommandText = ("insert into Hasta (TcKimlik,h_adı,h_soyadı,tel_no,ran_tar,ran_saati,sikayet_nedeni) values('" + textBox1.Text.ToString() + "','" + textBox2.Text.ToString() + "','" + textBox3.Text.ToString() + "','" + maskedTextBox1.Text.ToString() + "','" + dateTimePicker1.Text.ToString() + "','" + maskedTextBox2.Text.ToString() + "','" + textBox6.Text.ToString() + "')");
                    komut.ExecuteNonQuery();
                    MessageBox.Show("Kayıt Başarı İle Tamamlandı", "Kayıt Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


                }

            }
            baglanti.Close();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            maskedTextBox1.Clear();
            maskedTextBox2.Clear();
            textBox6.Clear();


        }
    }

I have tried a lot of ways but I could not go to a solution. If I check the data or make dr.Close() before registering the data, I can save the data, but then I get an error because I close the data reader prematurely. I want to check the data in the database but I get an error like below.

Associated with this Command, there is already an explicit DataReader that should be closed first.

Upvotes: 0

Views: 40

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176916

Issue with you code is you are making use of Command object for performing reading and inserting record at one time.

you require to perform operation one by one , you cannot do both in one time. so you code should be like this

using (OleDbconnection connection = new OleDbconnection (ConnectionString))
{
   connection.Open();
   OleDbCommand select = new OleDbCommand("..selecte query",connection);
   OleDbDataReader reader = select.ExecuteReader();

   if (reader.HasRows)
   {
      while (reader.Read())
      {
         //read data you want 

        //you might not need another connection , but i added to seperate it , you can reuse connection you created and perfrom update 
        using(OleDbconnection connection1 = new OleDbconnection (ConnectionString))
           {
               OleDbCommand insert= new OleDbCommand ("..insertquery", connection1);
            insert.ExecuteNonQuery();
           }
         }
      }

      reader.Close();
   }
}

you can use command object to perfrom multiple task but it should be one by one , example

//first complete reading and close reader 
//second do insert operation complete it 
//third do update operation

One more suggstion make use of Parameter as your insert query is open for sql injection attack, if you go with parameterise query can resolve that issue

Upvotes: 1

Related Questions