我一直在尝试查询一个表,如果条件为真,则执行另一个查询。
我收到以下异常:
System.Data.SqlClient.SqlException:必须声明标量变量“ @”
这是我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyServer"].ConnectionString);
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["MyServer"].ConnectionString);
int amounts = Convert.ToInt32(InstallmentPaidBox.Text) + Convert.ToInt32(InterestPaid.Text) + Convert.ToInt32(PenaltyPaid.Text);
int loans = Convert.ToInt32(PrincipalPaid.Text) + Convert.ToInt32(InterestPaid.Text);
con.Open();
DateTime date = Convert.ToDateTime(DateOfProcessing.Text);
int month1 = Convert.ToInt32(date.Month);
int year1 = Convert.ToInt32(date.Year);
SqlCommand a = new SqlCommand("Select Date,max(Amount)as Amount from Minimum_Amount group by Date",con);
SqlDataReader sq = a.ExecuteReader();
while (sq.Read()) {
DateTime date2 = Convert.ToDateTime(sq["Date"]);
int month2 = Convert.ToInt32(date2.Month);
int year2 = Convert.ToInt32(date2.Year);
if (month1 == month2 && year1 == year2)
{
int amount = Convert.ToInt32(sq["Amount"]);
string scrip = "<script>alert('Data Successfully Added')</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Added", scrip);
int areas = amount - Convert.ToInt32(InstallmentPaidBox.Text);
int forwarded = Convert.ToInt32(BalanceBroughtTextBox.Text) + Convert.ToInt32(InstallmentPaidBox.Text);
if (firsttime.Checked)
{
int balance = areas + Convert.ToInt32(PenaltyDue.Text) + Convert.ToInt32(InterestDue.Text);
SqlCommand cmd = new SqlCommand("insert into Cash_Position(Member_No,Welfare_Amount, BFWD,Amount,Installment_Paid,Loan_Repayment,Principal_Payment,Interest_Paid,Interest_Due,Loan_Balance,Penalty_Paid,Penalty_Due,Installment_Arrears,CFWD,Balance_Due,Date_Prepared,Prepared_By) values(@a,@b,@c,@d,@e,@)f,@g,@h,@i,@j,@k,@l,@m,@n,@o,@p,@q", con);
cmd.Parameters.Add("@a", SqlDbType.Int).Value = MemberNumberTextBox.Text;
cmd.Parameters.Add("@b", SqlDbType.Int).Value = WelfareAmount.Text;
cmd.Parameters.Add("@c", SqlDbType.Int).Value = BalanceBroughtTextBox.Text;
cmd.Parameters.Add("@d", SqlDbType.Int).Value = amounts;
cmd.Parameters.Add("@e", SqlDbType.Int).Value = InstallmentPaidBox.Text;
cmd.Parameters.Add("@f", SqlDbType.Int).Value = loans;
cmd.Parameters.Add("@g", SqlDbType.Int).Value = PrincipalPaid.Text;
cmd.Parameters.Add("@h", SqlDbType.Int).Value = InterestDue.Text;
cmd.Parameters.Add("@i", SqlDbType.Int).Value = InterestDue.Text;
cmd.Parameters.Add("@j", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@k", SqlDbType.Int).Value = PenaltyPaid.Text;
cmd.Parameters.Add("@l", SqlDbType.Int).Value = PenaltyDue.Text;
cmd.Parameters.Add("@m", SqlDbType.Int).Value = areas;
cmd.Parameters.Add("@n", SqlDbType.Int).Value = forwarded;
cmd.Parameters.Add("@o", SqlDbType.Int).Value = balance;
cmd.Parameters.Add("@p", SqlDbType.Date).Value = date;
cmd.Parameters.Add("@q", SqlDbType.Text).Value = PreparedBy.Text;
int rows = cmd.ExecuteNonQuery();
con.Close();
最佳答案
一个错误
错误
SqlCommand cmd = new SqlCommand("insert into Cash_Position(Member_No,Welfare_Amount, BFWD,Amount,Installment_Paid,Loan_Repayment,Principal_Payment,Interest_Paid,Interest_Due,Loan_Balance,Penalty_Paid,Penalty_Due,Installment_Arrears,CFWD,Balance_Due,Date_Prepared,Prepared_By) values(@a,@b,@c,@d,@e,@)f,@g,@h,@i,@j,@k,@l,@m,@n,@o,@p,@q", con);
前一个是
@)f
,更正后是-> @f
前一个是
@q
,更正后是-> @q)
正确
SqlCommand cmd = new SqlCommand("insert into Cash_Position(Member_No,Welfare_Amount, BFWD,Amount,Installment_Paid,Loan_Repayment,Principal_Payment,Interest_Paid,Interest_Due,Loan_Balance,Penalty_Paid,Penalty_Due,Installment_Arrears,CFWD,Balance_Due,Date_Prepared,Prepared_By) values(@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n,@o,@p,@q)", con);