我正在尝试向数据库添加新值,但执行查询不起作用。我已经测试并意识到在执行非查询后,系统停止工作。简而言之,它不会发射。没有返回错误。这是代码:
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();
}
答案 0 :(得分:1)
您无法在与开放数据阅读器关联的连接上执行任何命令。您必须关闭阅读器或使用其他连接。因为您的阅读器仅供开放阅读器使用。
<小时/> 顺便说一下,您应该抓住
System.InvalidOperationException
详细信息,
已经有一个与此命令关联的开放DataReader 必须先关闭。
Here is你可以做什么。
答案 1 :(得分:0)
尝试在while语句附近放置一个断点并对其进行调试。或者你可以在函数内部进行操作。如果Minimum_Amount表中没有行,则执行可能无法到达while循环内的代码段。