There is already an open Datareader associated in C#

Posted by Celine on Stack Overflow See other posts from Stack Overflow or by Celine
Published on 2013-10-18T15:21:55Z Indexed on 2013/10/18 15:54 UTC
Read the original article Hit count: 294

Filed under:
|

I can't seem to find the reason behind this error. Can someone look into my codes?

private void button2_Click_1(object sender, EventArgs e)
{
    con.Open();

    string check
        = "Select block, section, size from interment_location where block='"
          + textBox12.Text
          + "' and section='"
          + textBox13.Text
          + "' and size='"
          + textBox14.Text
          + "'";

    SqlCommand cmd1 = new SqlCommand(check, con);
    SqlDataReader rdr;
    rdr = cmd1.ExecuteReader();

    try
    {
        if (textBox1.Text == ""
            || textBox2.Text == ""
            || textBox3.Text == ""
            || textBox4.Text == ""
            || textBox7.Text == ""
            || textBox8.Text == ""
            || textBox9.Text == ""
            || textBox10.Text == ""
            || dateTimePicker1.Value.ToString("yyyyMMdd HH:mm:ss") == ""
            || dateTimePicker2.Value.ToString("yyyyMMdd HH:mm:ss") == ""
            || textBox11.Text == ""
            || dateTimePicker3.Value.ToString("yyyyMMdd HH:mm:ss") == ""
            || textBox12.Text == ""
            || textBox13.Text == ""
            || textBox14.Text == "")
        {
            MessageBox.Show(
                "Please input a value!",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation);
        }
        else if (rdr.HasRows == true)
        {
            MessageBox.Show("Interment Location is already reserved.");
            textBox12.Clear();
            textBox13.Clear();
            textBox14.Clear();
            textBox12.Focus();
        }
        else if (MessageBox.Show(
            "Are you sure you want to reserve this record?",
            "Reserve",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Question) == DialogResult.Yes)
        {
            SqlCommand cmd4 = new SqlCommand(
                "insert into owner_info(ownerf_name, ownerm_name,"
                    + " ownerl_name, home_address, tel_no, office)"
                    + " values('"
                    + textBox1.Text
                    + "', '"
                    + textBox2.Text
                    + "', '"
                    + textBox3.Text
                    + "', '"
                    + textBox4.Text
                    + "', '"
                    + textBox7.Text
                    + "', '"
                    + textBox8.Text
                    + "')",
                con);

            cmd.ExecuteNonQuery();

            SqlCommand cmd5 = new SqlCommand(
                "insert into deceased_info(deceased_id, name_of_deceased, "
                    + "address, do_birth, do_death, place_of_death, "
                    + "date_of_interment, COD_id, place_of_vigil_id, "
                    + "service_id, owner_id) values('"
                    + ownerid
                    + "','"
                    + textBox9.Text
                    + "', '"
                    + textBox10.Text
                    + "', '"
                    + dateTimePicker1.Value.ToString("yyyyMMdd HH:mm:ss")
                    + "', '"
                    + dateTimePicker2.Value.ToString("yyyyMMdd HH:mm:ss")
                    + "', '"
                    + textBox11.Text
                    + "', '"
                    + dateTimePicker3.Value.ToString("yyyyMMdd HH:mm:ss")
                    + "', '"
                    + ownerid
                    + "', '"
                    + ownerid
                    + "', '"
                    + ownerid
                    + "', '"
                    + ownerid
                    + "')",
                con); 

            cmd.ExecuteNonQuery();

            SqlCommand cmd6 = new SqlCommand(
                "insert into interment_location(lot_no, block, section, "
                    + "size, garden_id) values('"
                    + ownerid
                    + "','"
                    + textBox12.Text
                    + "', '"
                    + textBox13.Text
                    + "', '"
                    + textBox14.Text
                    + "', '"
                    + ownerid
                    + "')",
                con);

            cmd.ExecuteNonQuery();

            MessageBox.Show(
                "Your reservation has been made!",
                "Reserve",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information); 
        }
    }
    catch (Exception x)
    {
        MessageBox.Show(x.Message);
    }

    con.Close();    
}

This is the only code that I edited after.

© Stack Overflow or respective owner

Related posts about c#

Related posts about datareader