Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (planDS.Tables.Contains("CloudBackupSchedule"))
                    {
                        dt = planDS.Tables["CloudBackupSchedule"];
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            //Get field names
                            string sqlString = "UPDATE CloudBackupSchedule SET ";
                            var sqlParams = new string[dt.Rows[0].ItemArray.Count() + 1];
                            int count = 0;
                            foreach (DataColumn dc in dt.Columns)
                            {
                                if (dc.ColumnName.ToLower() == "baseplan_id")
                                {
                                    sqlString += "PlanId = @PlanId, ";
                                    sqlParams[count] = "@PlanId";
                                    count++;

                                    sqlString += "BackupScheduleId = @BackupScheduleId,";
                                    sqlParams[count] = "@BackupScheduleId";
                                    count++;
                                }
                                else
                                {

                                    sqlString += string.Format("{0} = @{0},", dc.ColumnName);
                                    sqlParams[count] = string.Format("@{0}", dc.ColumnName);
                                    count++;
                                }


                            }
                            //create query from fields                            
                            sqlString = sqlString.TrimEnd(new char[] { ',', (char)32 }) + " WHERE Planid = @planid And BackupScheduleId = @BackupScheduleId";
                            cGlobalSettings.oLogger.WriteLog(string.Format("Query={0}", sqlString));


                            sqlCmd.CommandText = sqlString;

                            foreach (DataRow dr in dt.Rows)
                            {
                                for (int i = 0; i < sqlParams.Length; i++)
                                {
                                    if (sqlParams[i] != null)
                                    {
                                        if (sqlParams[i].TrimStart(new char[] { '@' }) == "PlanId")
                                        {
                                            sqlCmd.Parameters.AddWithValue(sqlParams[i], planid);
                                            cGlobalSettings.oLogger.WriteLog(string.Format("{2}. Parameter={0},Value={1}", sqlParams[i], planid, i));
                                        }
                                        else if (sqlParams[i].TrimStart(new char[] { '@' }) == "BackupScheduleId")
                                        {
                                            sqlCmd.Parameters.AddWithValue(sqlParams[i], planid);
                                            cGlobalSettings.oLogger.WriteLog(string.Format("{2}. Parameter={0},Value={1}", sqlParams[i], planid, i));
                                        }
                                        else
                                        {
                                            sqlCmd.Parameters.AddWithValue(sqlParams[i], dr[sqlParams[i].TrimStart(new char[] { '@' })] ?? DBNull.Value);
                                            cGlobalSettings.oLogger.WriteLog(string.Format("{2}. Parameter={0},Value={1}", sqlParams[i], dr[sqlParams[i].TrimStart(new char[] { '@' })] ?? "NoValue", i));
                                        }
                                    }

                                }

                                try
                                {
                                    rowsAffected = 0;
                                    rowsAffected = sqlCmd.ExecuteNonQuery();
                                    cGlobalSettings.oLogger.WriteLog(string.Format("Rows affected= {0}", rowsAffected));
                                }
                                catch (Exception ex)
                                {
                                    cGlobalSettings.oLogger.WriteLogException("cSqliteFunction.cs::ExecuteNonQuery() ", ex);
                                }
                                
                                sqlCmd.Parameters.Clear();
                            }

                        }
                    }


log information

12-08-2016 17:36:02.128|Query=UPDATE CloudBackupSchedule SET BackupScheduleType = @BackupScheduleType,ScheduleSpecificDate = @ScheduleSpecificDate,ScheduleSpecificTime = @ScheduleSpecificTime,ScheduleRecurringType = @ScheduleRecurringType,DailyOccursAtTime = @DailyOccursAtTime,DailyOccursEvery = @DailyOccursEvery,DailyOccursEveryType = @DailyOccursEveryType,DailyOccursEveryFrom = @DailyOccursEveryFrom,DailyOccursEveryTo = @DailyOccursEveryTo,WeeklyRecurringDays = @WeeklyRecurringDays,MonthlyOccuranceType = @MonthlyOccuranceType,MonthlyDaysOfMonth = @MonthlyDaysOfMonth,MonthlyOccuranceDays = @MonthlyOccuranceDays,MonthlyOccuranceRepeatMonth = @MonthlyOccuranceRepeatMonth,MonthlyOccuranceStartFrom = @MonthlyOccuranceStartFrom,YearlyOccuranceRepeatYear = @YearlyOccuranceRepeatYear,YearlyOccuranceStartFrom = @YearlyOccuranceStartFrom,IsScheduleStopPlan = @IsScheduleStopPlan,ScheduleStopPlanHourMinute = @ScheduleStopPlanHourMinute,ScheduleStopPlanType = @ScheduleStopPlanType,IsScheduleMissedPlan = @IsScheduleMissedPlan,CreatedDate = @CreatedDate,UpdatedDate = @UpdatedDate,PlanId = @PlanId, BackupScheduleId = @BackupScheduleId WHERE Planid = @planid And BackupScheduleId = @BackupScheduleId
12-08-2016 17:36:02.130|0. Parameter=@BackupScheduleType,Value=0
12-08-2016 17:36:02.132|1. Parameter=@ScheduleSpecificDate,Value=
12-08-2016 17:36:02.133|2. Parameter=@ScheduleSpecificTime,Value=
12-08-2016 17:36:02.135|3. Parameter=@ScheduleRecurringType,Value=0
12-08-2016 17:36:02.136|4. Parameter=@DailyOccursAtTime,Value=
12-08-2016 17:36:02.138|5. Parameter=@DailyOccursEvery,Value=0
12-08-2016 17:36:02.139|6. Parameter=@DailyOccursEveryType,Value=
12-08-2016 17:36:02.141|7. Parameter=@DailyOccursEveryFrom,Value=
12-08-2016 17:36:02.142|8. Parameter=@DailyOccursEveryTo,Value=
12-08-2016 17:36:02.144|9. Parameter=@WeeklyRecurringDays,Value=
12-08-2016 17:36:02.145|10. Parameter=@MonthlyOccuranceType,Value=0
12-08-2016 17:36:02.146|11. Parameter=@MonthlyDaysOfMonth,Value=0
12-08-2016 17:36:02.148|12. Parameter=@MonthlyOccuranceDays,Value=
12-08-2016 17:36:02.149|13. Parameter=@MonthlyOccuranceRepeatMonth,Value=0
12-08-2016 17:36:02.151|14. Parameter=@MonthlyOccuranceStartFrom,Value=
12-08-2016 17:36:02.152|15. Parameter=@YearlyOccuranceRepeatYear,Value=0
12-08-2016 17:36:02.153|16. Parameter=@YearlyOccuranceStartFrom,Value=
12-08-2016 17:36:02.154|17. Parameter=@IsScheduleStopPlan,Value=0
12-08-2016 17:36:02.156|18. Parameter=@ScheduleStopPlanHourMinute,Value=0
12-08-2016 17:36:02.157|19. Parameter=@ScheduleStopPlanType,Value=
12-08-2016 17:36:02.158|20. Parameter=@IsScheduleMissedPlan,Value=0
12-08-2016 17:36:02.160|21. Parameter=@CreatedDate,Value=05-08-2016 17:01:28
12-08-2016 17:36:02.161|22. Parameter=@UpdatedDate,Value=
12-08-2016 17:36:02.162|23. Parameter=@PlanId,Value=1
12-08-2016 17:36:02.163|24. Parameter=@BackupScheduleId,Value=1

What I have tried:

iam have matched all the parameters in query but still getting the error
Posted
Updated 12-Aug-16 4:40am
v2
Comments
F-ES Sitecore 12-Aug-16 8:28am    
Use the debugger to examine the SQL being executed and also the parameters collection. There will probably be a mismatch somewhere, and as we can't run your code as we don't have access to your data it's hard to debug remotely.
srilekhamenon 12-Aug-16 9:21am    
does the sequence of parameter matter ?

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Run this code on debugger, when the error arise, check which parameter you have and what is your query.
 
Share this answer
 
The order of parameters is not important when you use AddWithValue. The first parameter to AddWithValue is the name of the parameter so therefore the order is no longer important.

The error simply means that your SQL is expecting more parameters than you gave it. All you have to do is walk through the code and make sure it is doing what you expect it to. It is pretty easy to fix on your own.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900