0

我似乎找不到这个错误背后的原因。有人可以查看我的代码吗?

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();    
}

这是我之后编辑的唯一代码。

4

2 回答 2

0

试试下面的代码,看看它是否适合你。

 else if (rdr.HasRows == true)
    {
        MessageBox.Show("Interment Location is already reserved.");
        textBox12.Clear();
        textBox13.Clear();
        textBox14.Clear();
        textBox12.Focus();
    }
rdr.close() // close your reader here!
于 2013-10-18T16:36:50.547 回答
0
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)
    {
        if (!rdr.IsClosed) 
                rdr.Close(); //close the data reader
        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);
}
if (!rdr.IsClosed)
    rdr.Close(); //close the datareader if it is not closed already
con.Close();    

}

于 2013-10-18T15:50:38.447 回答