Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Insertion

$
0
0

Good day all,

I am trying to get this logic done but facing some problem.

The logic is that at a click of a button i select all student who are in a particular class from the students table and insert them into a studentaccount table attaching fees amount.

But before i do the individual insertion i want to check if a student has a balance to pay and if he does i had it up to the fees before inserting.

Below is my code for review

protected void SubmitAccountContent()
    {
        SqlConnection conn = new SqlConnection(strconnection);
        SqlCommand cmd = new SqlCommand("SELECT StudID, ClassNo FROM Students WHERE LevelName = @LevelName AND ClassNo = @ClassNo", conn);
        {
            cmd.Parameters.AddWithValue("@LevelName", RadComboBoxFeeLevel.SelectedValue);
            cmd.Parameters.AddWithValue("@ClassNo", RadComboBoxFeeClass.SelectedValue);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                SqlConnection conn2 = new SqlConnection(strconnection);
                SqlCommand cmd2 = new SqlCommand("SELECT Balance FROM StudentAccount WHERE StudID = @StudID", conn);
                {
                    cmd2.Parameters.AddWithValue("@StudID", Convert.ToString(dr["StudID"]));
                    conn2.Open();
                    SqlDataReader dr2 = cmd.ExecuteReader();
                    while (dr2.Read())
                    {
                        RadNumericTextBoxBalance.Text = dr["Fees"].ToString();
                    }
                    conn2.Close();

                }


                Decimal QFees = decimal.Parse(RadNumericTextBoxFees.Text);
                Decimal QBal = decimal.Parse(RadNumericTextBoxBalance.Text);

                Decimal QBalance = QFees + QBal;


                SqlConnection conn1 = new SqlConnection(strconnection);
                conn1.Open();
                SqlCommand cmd1 = new SqlCommand("INSERT INTO  StudentAccount(StudID, Amount, Balance, AcademicYear, TermName, ClassNo) VALUES (@StudID, @Amount, @Balance, @AcademicYear, @TermName, @ClassNo)", conn1);
                cmd1.Parameters.AddWithValue("@StudID", Convert.ToString(dr["StudID"]));
                cmd1.Parameters.AddWithValue("@Amount", RadNumericTextBoxFees.Text);
                cmd1.Parameters.AddWithValue("@Balance", QBalance);
                cmd1.Parameters.AddWithValue("@AcademicYear", RadComboBoxAcademicYear.SelectedValue);
                cmd1.Parameters.AddWithValue("@TermName", RadComboBoxTerm.SelectedValue);
                cmd1.Parameters.AddWithValue("@ClassNo", Convert.ToString(dr["ClassNo"]));
                cmd1.ExecuteNonQuery();
                conn1.Close();
            }
            conn.Close();

        }
    }

and i get this error too

There is already an open DataReader associated with this Command which must be closed first.


Line 137:                    cmd2.Parameters.AddWithValue("@StudID", Convert.ToString(dr["StudID"]));
Line 138:                    conn2.Open();
Line 139:                    SqlDataReader dr2 = cmd.ExecuteReader();
Line 140:                    while (dr2.Read())
Line 141:                    {

Any help or advice.

Thanks


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>