Reputation: 49
I am trying to add new values to database but executenonquery is not working. I have tested and realized that just after executing non query, the system stops working. In short, it does not fire. There is no error returned. Here is the code:
else
{
double amounts = Convert.ToDouble(InstallmentPaidBox.Text);
string date = period.Text;
SqlCommand a = new SqlCommand("Select top 1* from Minimum_Amount order by Period desc", con);
con.Open();
SqlDataReader sq = a.ExecuteReader();
while (sq.Read())
{
string date2 = sq["Period"].ToString();
double amount = Convert.ToDouble(sq["Amount"]);
double areas = amount - (Convert.ToDouble(InstallmentPaidBox.Text) + Convert.ToDouble(BalanceBroughtTextBox.Text));
double forwarded = Convert.ToDouble(BalanceBroughtTextBox.Text) + Convert.ToDouble(InstallmentPaidBox.Text);
double balance = areas;
SqlCommand cmd = new SqlCommand("insert into Cash_Position(Member_No,Welfare_Amount, BFWD,Amount,Installment_Paid,Loan_Repayment,Principal_Paid,Loan_Balance,Interest_Paid,Interest_Due,Penalty_Paid,Penalty_Due,Installment_Arrears,CFWD,Balance_Due,Period,Date_Prepared,Prepared_By) values(@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n,@o,@x,@p,@q)", con);
cmd.Parameters.Add("@a", SqlDbType.NChar).Value = MemberNumberTextBox.Text;
cmd.Parameters.Add("@b", SqlDbType.Money).Value = WelfareAmount.Text;
cmd.Parameters.Add("@c", SqlDbType.Money).Value = BalanceBroughtTextBox.Text;
cmd.Parameters.Add("@d", SqlDbType.Money).Value = amounts;
cmd.Parameters.Add("@e", SqlDbType.Money).Value = InstallmentPaidBox.Text;
cmd.Parameters.Add("@f", SqlDbType.Money).Value = 0;
cmd.Parameters.Add("@g", SqlDbType.Money).Value = PrincipalPaid.Text;
cmd.Parameters.Add("@h", SqlDbType.Money).Value = 0;
cmd.Parameters.Add("@i", SqlDbType.Money).Value = InterestPaid.Text;
cmd.Parameters.Add("@j", SqlDbType.Money).Value = 0;
cmd.Parameters.Add("@k", SqlDbType.Money).Value = PenaltyPaid.Text;
cmd.Parameters.Add("@l", SqlDbType.Money).Value = 0;
cmd.Parameters.Add("@m", SqlDbType.Money).Value = areas;
cmd.Parameters.Add("@n", SqlDbType.Money).Value = forwarded;
cmd.Parameters.Add("@o", SqlDbType.Money).Value = balance;
cmd.Parameters.Add("@x", SqlDbType.NChar).Value = period.Text;
cmd.Parameters.Add("@p", SqlDbType.Date).Value = dateOf.Text;
cmd.Parameters.Add("@q", SqlDbType.VarChar).Value = prepared.Text;
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
string script = "<script>alert('Data Successfully Added')</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Added", script);
}
else
{
string script = "<script>alert('Error Adding Data')</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", script);
}
}
con.Close();
}
Upvotes: 0
Views: 1795
Reputation: 5986
You cannot execute any commands on a connection which is associated with an open data reader. You have to close the reader or use another connection. Because your reader is used by the open reader exclusively.
System.InvalidOperationException
with detail,
There is already an open DataReader associated with this Command which must be closed first.
Here is what you can do.
Upvotes: 2
Reputation: 1038
Try putting a breakpoint near the while statement and do debugging on that. Or you can do a step-into inside the function. If there are no rows in the Minimum_Amount table ,the execution may not reach your code snippet inside the while loop.
Upvotes: 1