1

I have a problem with my simple C# application. When I loop the datagridview row count, then insert the data accordingly it throws the following error after looping until 58:

the incoming request has too many parameters...

But i had checked a few time with the loop and query and seem like nothing seem wrong.

for (int i = 0; i < dailyReportGridView.Rows.Count - 1; i++)
{
    StrQuery =
        @"INSERT INTO TIME_ATTENDANCE_REPORT VALUES (@CHECKTIME" + i + ", @BADGENUMBER" + i + ", @NAME" + i +
        ", @IN1" + i + ", @OUT1" + i + ", @IN2" + i + ", @OUT2" + i + ", @IN3" + i + ", @OUT3" + i +
        ", @IN4" + i + ", @OUT4" + i + ", @IN5" + i + ", @OUT5" + i + ", @WORKDAY" + i + ", @OT" + i + ", @SUNDAYHOUR" + i +
        ", @SUNDAYOT" + i + ", @PHWORK" + i + ", @PHOT" + i + ", @LEAVE" + i + ", @REMARK" + i + ", @LEAVENAME" + i + ", @DEPT" + i + ", @STARTSPECDAY" + i +
        ", @ENDSPECDAY" + i + ", @EDITED" + i + ", @EDITREASON" + i + ", @DEDUCTION" + i + ", @DOUBLESHIFT" + i +
        ", @TOTALAL" + i + ", @TOTALUL" + i + ", @TOTALSL" + i + ", @TOTALOL" + i + ", @TOTALML" + i + ", @TOTALBT" + i + ", @SYNCBY" + i + ")";
    comm.Parameters.Add("@CHECKTIME" + i, SqlDbType.DateTime);
    comm.Parameters["@CHECKTIME" + i].Value = dailyReportGridView.Rows[i].Cells["DATE"].Value;
    comm.Parameters.Add(new SqlParameter("BADGENUMBER" + i, dailyReportGridView.Rows[i].Cells["BADGENUMBER"].Value));
    comm.Parameters.Add(new SqlParameter("NAME" + i, dailyReportGridView.Rows[i].Cells["NAME"].Value));
    comm.Parameters.Add(new SqlParameter("IN1" + i, dailyReportGridView.Rows[i].Cells["IN_1"].Value));
    comm.Parameters.Add(new SqlParameter("OUT1" + i, dailyReportGridView.Rows[i].Cells["OUT_1"].Value));
    comm.Parameters.Add(new SqlParameter("IN2" + i, dailyReportGridView.Rows[i].Cells["IN_2"].Value));
    comm.Parameters.Add(new SqlParameter("OUT2" + i, dailyReportGridView.Rows[i].Cells["OUT_2"].Value));
    comm.Parameters.Add(new SqlParameter("IN3" + i, dailyReportGridView.Rows[i].Cells["IN_3"].Value));
    comm.Parameters.Add(new SqlParameter("OUT3" + i, dailyReportGridView.Rows[i].Cells["OUT_3"].Value));
    comm.Parameters.Add(new SqlParameter("IN4" + i, dailyReportGridView.Rows[i].Cells["IN_4"].Value));
    comm.Parameters.Add(new SqlParameter("OUT4" + i, dailyReportGridView.Rows[i].Cells["OUT_4"].Value));
    comm.Parameters.Add(new SqlParameter("IN5" + i, dailyReportGridView.Rows[i].Cells["IN_5"].Value));
    comm.Parameters.Add(new SqlParameter("OUT5" + i, dailyReportGridView.Rows[i].Cells["OUT_5"].Value));
    comm.Parameters.Add(new SqlParameter("WORKDAY" + i, dailyReportGridView.Rows[i].Cells["WorkDay"].Value));
    comm.Parameters.Add(new SqlParameter("OT" + i, dailyReportGridView.Rows[i].Cells["OT"].Value));
    comm.Parameters.Add(new SqlParameter("SUNDAYHOUR" + i, dailyReportGridView.Rows[i].Cells["SundayHour"].Value));
    comm.Parameters.Add(new SqlParameter("SUNDAYOT" + i, dailyReportGridView.Rows[i].Cells["SundayOT"].Value));
    comm.Parameters.Add(new SqlParameter("PHWORK" + i, dailyReportGridView.Rows[i].Cells["PH(hr)"].Value));
    comm.Parameters.Add(new SqlParameter("PHOT" + i, dailyReportGridView.Rows[i].Cells["PH OT(hr)"].Value));
    comm.Parameters.Add(new SqlParameter("LEAVE" + i, dailyReportGridView.Rows[i].Cells["Leave"].Value));
    comm.Parameters.Add(new SqlParameter("REMARK" + i, dailyReportGridView.Rows[i].Cells["Remark"].Value));
    comm.Parameters.Add(new SqlParameter("LEAVENAME" + i, dailyReportGridView.Rows[i].Cells["LeaveName"].Value));
    comm.Parameters.Add(new SqlParameter("DEPT" + i, dailyReportGridView.Rows[i].Cells["Dept"].Value));
    comm.Parameters.Add(new SqlParameter("STARTSPECDAY" + i, dailyReportGridView.Rows[i].Cells["StartSpecDay"].Value));
    comm.Parameters.Add(new SqlParameter("ENDSPECDAY" + i, dailyReportGridView.Rows[i].Cells["EndSpecDay"].Value));
    comm.Parameters.Add(new SqlParameter("EDITED" + i, dailyReportGridView.Rows[i].Cells["Edited"].Value));
    comm.Parameters.Add(new SqlParameter("EDITREASON" + i, dailyReportGridView.Rows[i].Cells["Edit Reason"].Value));
    comm.Parameters.Add(new SqlParameter("DEDUCTION" + i, dailyReportGridView.Rows[i].Cells["Deduction"].Value));
    comm.Parameters.Add(new SqlParameter("DOUBLESHIFT" + i, dailyReportGridView.Rows[i].Cells["Double Shift"].Value));
    comm.Parameters.Add(new SqlParameter("TOTALAL" + i, dailyReportGridView.Rows[i].Cells["TotalAL"].Value));
    comm.Parameters.Add(new SqlParameter("TOTALUL" + i, dailyReportGridView.Rows[i].Cells["TotalUL"].Value));
    comm.Parameters.Add(new SqlParameter("TOTALSL" + i, dailyReportGridView.Rows[i].Cells["TotalSL"].Value));
    comm.Parameters.Add(new SqlParameter("TOTALOL" + i, dailyReportGridView.Rows[i].Cells["TotalOL"].Value));
    comm.Parameters.Add(new SqlParameter("TOTALML" + i, dailyReportGridView.Rows[i].Cells["Maternity"].Value));
    comm.Parameters.Add(new SqlParameter("TOTALBT" + i, dailyReportGridView.Rows[i].Cells["BusinessTrip"].Value));
    comm.Parameters.Add(new SqlParameter("SYNCBY" + i, lblUserName.Text.ToString()));
    comm.CommandText = StrQuery;
    comm.ExecuteNonQuery();
}

Somehow the error is on the line of code comm.ExecuteNonQuery(). Any solution to solve it?

1
  • before "strquery =" try this "comm = new SqlCommand ..." Commented Dec 7, 2016 at 14:26

1 Answer 1

4

Add comm.Parameters.Clear(); after comm.ExecuteNonQuery() - it should be executed each iteration. Otherwise you add the parameters multiple times.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.