带有ParameterName' @ d23'的SqlParameter不包含在此sqlparametercollection

时间:2016-03-24 14:53:12

标签: c#

我正在为CommandParameters集合添加参数以便重复执行;每个数据网格行一次。

我收到了这个例外。

  

"带有ParameterName' @ dx'的SqlParameter ( x代表一个参数   名称) 不包含在此SqlParameterCollection"

我做错了什么,在哪里?

try
{
   con = new SqlConnection(cs.DBConn);
   con.Open();

    string cb = "insert into feepayment(FeePaymentID,ScholarNo,FeeID,FDCourse,FDBranch,Semester,TutionFees,LibraryFees,catfees,stationaryfees,textbooksfees,swimmingfees,boardingfees,languagesfees,mealsfees,othersfees,computerfees,activityfees,insurancefees,orphansfees,diaryfees,furniturefees,maintenancefees,TotalFees,DateOfPayment,ModeOfPayment,PaymentModeDetails,TotalPaid,Fine,DueFees,PreviousTerm) VALUES (@d23,@d24,@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17,@d18,@d19,@d20,@d21,@d22,@d25,@d26,@d27,@d28,@d29,@d30,@d31,@d32,@d33)";

    cmd1 = new SqlCommand(cb);

    cmd1.Connection = con;
    // Add Parameters to Command Parameters collection
    cmd1.Parameters.Add(new SqlParameter("@d23", System.Data.SqlDbType.NChar, 200, "FeePaymentID"));
    cmd1.Parameters.Add(new SqlParameter("@d24", System.Data.SqlDbType.NChar, 15, "ScholarNo"));
    cmd1.Parameters.Add(new SqlParameter("@d1", System.Data.SqlDbType.NChar, 20, "FeeId"));
    cmd1.Parameters.Add(new SqlParameter("@d2", System.Data.SqlDbType.NChar, 20, "FDCourse"));
    cmd1.Parameters.Add(new SqlParameter("@d3", System.Data.SqlDbType.NChar, 30, "FDBranch"));
    cmd1.Parameters.Add(new SqlParameter("@d4", System.Data.SqlDbType.NChar, 10, "Semester"));
    cmd1.Parameters.Add(new SqlParameter("@d5", System.Data.SqlDbType.Int, 10, "TutionFees"));
    cmd1.Parameters.Add(new SqlParameter("@d6", System.Data.SqlDbType.Int, 10, "LibraryFees"));
    cmd1.Parameters.Add(new SqlParameter("@d7", System.Data.SqlDbType.Int, 10, "catfees"));
    cmd1.Parameters.Add(new SqlParameter("@d8", System.Data.SqlDbType.Int, 15, "stationaryfees"));
    cmd1.Parameters.Add(new SqlParameter("@d9", System.Data.SqlDbType.Int, 10, "textbookfees"));
    cmd1.Parameters.Add(new SqlParameter("@d10", System.Data.SqlDbType.Int, 10, "swimmingfees"));
    cmd1.Parameters.Add(new SqlParameter("@d11", System.Data.SqlDbType.Int, 10, "boardingfees"));
    cmd1.Parameters.Add(new SqlParameter("@d12", System.Data.SqlDbType.Int, 10, "languagesfees"));
    cmd1.Parameters.Add(new SqlParameter("@d13", System.Data.SqlDbType.Int, 10, "mealsfees"));
    cmd1.Parameters.Add(new SqlParameter("@d14", System.Data.SqlDbType.Int, 10, "othersfees"));
    cmd1.Parameters.Add(new SqlParameter("@d15", System.Data.SqlDbType.Int, 10, "computerfees"));
    cmd1.Parameters.Add(new SqlParameter("@d16", System.Data.SqlDbType.Int, 10, "activityfees"));
    cmd1.Parameters.Add(new SqlParameter("@d17", System.Data.SqlDbType.Int, 10, "insurancefees"));
    cmd1.Parameters.Add(new SqlParameter("@d18", System.Data.SqlDbType.Int, 10, "orphansfees"));
    cmd1.Parameters.Add(new SqlParameter("@d19", System.Data.SqlDbType.Int, 10, "diaryfees"));
    cmd1.Parameters.Add(new SqlParameter("@d20", System.Data.SqlDbType.Int, 10, "furniturefees"));
    cmd1.Parameters.Add(new SqlParameter("@d21", System.Data.SqlDbType.Int, 10, "maintenancefees"));
    cmd1.Parameters.Add(new SqlParameter("@d22", System.Data.SqlDbType.Int, 10, "TotalFees"));
    cmd1.Parameters.Add(new SqlParameter("@d25", System.Data.SqlDbType.NChar, 30, "DateOfPayment"));
    cmd1.Parameters.Add(new SqlParameter("@d26", System.Data.SqlDbType.NChar, 20, "ModeOfPayment"));
    cmd1.Parameters.Add(new SqlParameter("@d27", System.Data.SqlDbType.VarChar, 200, "PaymentModeDetails"));
    cmd1.Parameters.Add(new SqlParameter("@d28", System.Data.SqlDbType.Int, 10, "TotalPaid"));
    cmd1.Parameters.Add(new SqlParameter("@d29", System.Data.SqlDbType.Int, 10, "Fine"));
    cmd1.Parameters.Add(new SqlParameter("@d30", System.Data.SqlDbType.Int, 10, "DueFees"));
    cmd1.Parameters.Add(new SqlParameter("@d31", System.Data.SqlDbType.Int, 10, "RegistrationFees"));
    cmd1.Parameters.Add(new SqlParameter("@d32", System.Data.SqlDbType.Int, 10, "MusicFees"));
    cmd1.Parameters.Add(new SqlParameter("@d33", System.Data.SqlDbType.Int, 10, "PreviousTerm"));

    // Prepare command for repeated execution
    cmd1.Prepare();
    // Data to be inserted
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {

        if (!row.IsNewRow)
        {
            invoice(row.Cells[0].Value.ToString(), row.Cells[4].Value.ToString());

            cmd1.Parameters["@d23"].Value = paymentID;
            cmd1.Parameters["@d24"].Value = row.Cells[0].Value.ToString();

            cmd1.Parameters["@d1"].Value = row.Cells[4].Value.ToString();
            cmd1.Parameters["@d2"].Value = course;
            cmd1.Parameters["@d3"].Value = branch;
            cmd1.Parameters["@d4"].Value = sem;
            cmd1.Parameters["@d5"].Value = tution;
            cmd1.Parameters["@d6"].Value = library;
            cmd1.Parameters["@d7"].Value = cat;
            cmd1.Parameters["@d8"].Value = stationary;
            cmd1.Parameters["@d9"].Value = textbook;
            cmd1.Parameters["@d10"].Value = swimming;

            if (row.Cells[3].Value.Equals("Boarder"))
            {
                cmd1.Parameters["@d11"].Value = boarding.ToString();

                cmd1.Parameters["@d22"].Value = totals.ToString();
            }
            else if (row.Cells[3].Value.Equals("Commuter"))
            {

                cmd1.Parameters["@d11"].Value = "0";
                cmd1.Parameters["@d22"].Value = (totals - boarding).ToString();

            }

            cmd1.Parameters["@d12"].Value = languages;
            cmd1.Parameters["@d13"].Value = meals;
            cmd1.Parameters["@d14"].Value = others;
            cmd1.Parameters["@d15"].Value = computer;
            cmd1.Parameters["@d16"].Value = activity;
            cmd1.Parameters["@d17"].Value = insurance;
            cmd1.Parameters["@d18"].Value = orphans;
            cmd1.Parameters["@d19"].Value = diary;
            cmd1.Parameters["@d20"].Value = furniture;
            cmd1.Parameters["@d21"].Value = maintenance;
            cmd1.Parameters["@d25"].Value = effectivedate.Text;
            cmd1.Parameters["@d26"].Value = "";
            cmd1.Parameters["@d27"].Value = "";
            cmd1.Parameters["@d28"].Value = 0;
            cmd1.Parameters["@d29"].Value = 0;
            cmd1.Parameters["@d30"].Value = 0;
            cmd1.Parameters["@d31"].Value = registration;
            cmd1.Parameters["@d32"].Value = music;
            cmd1.Parameters["@d33"].Value = int.Parse(prevdue);

            cmd1.ExecuteNonQuery();
        }
    }
    con.Close();
    MessageBox.Show("Successfully saved - r", "Entry", MessageBoxButtons.OK, MessageBoxIcon.Information);
    btnSave.Enabled = false;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error>>>>", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

2 个答案:

答案 0 :(得分:1)

当您发帖时,请不要只转储所有代码,只提供重要信息。

我看到的插入列表中没有D41,而DBugger说它应该是cmd1

 cmd1.Parameters.Add(new SqlParameter("@d41", System.Data.SqlDbType.NChar, 10, "Semester"));

修改

您缺少的另一件事是您在insert语句中缺少一个参数,有34列,只有33个值。你需要@ d34。在插入语句的末尾

理想情况下,您应该将所有信息放在某种结构中,并在循环中添加参数,并使用字符串生成器构造该insert语句。它将清理您的代码并使其更容易调试

答案 1 :(得分:1)

在插入声明中缺少@ d41

VALUES (@d23,@d24,@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,
@d17,@d18,@d19,@d20,@d21,@d22,@d25,@d26,@d27,@d28,@d29,@d30,@d31,@d32,@d33)

您正在添加参数" @ d41"到cmd而不是cmd1

cmd.Parameters.Add(new SqlParameter("@d41", System.Data.SqlDbType.NChar, 10, "Semester"));

在你的for循环中,你试图为" @ d41"添加一个值。对于命令cmd1

,它不存在
cmd1.Parameters["@d41"].Value = "";