3

1 つのテーブルにクエリを実行しようとしていて、条件が true の場合は別のクエリを実行します。

次の例外が発生します。

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

1 に答える 1

3

1つの間違い

違う

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);
于 2012-09-24T09:36:57.963 に答える