簡體   English   中英

“InvalidArgument=‘1’的值對‘index’無效。 參數名稱:index”,顯示第二個數據索引並插入數據庫時出現

[英]“InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index”, occurs when the second data index is displayed and nserted to database

問候,
我有一個 function 從考勤機中提取數據以顯示並存儲在數據庫中,然后從機器中刪除數據,,

然后,當第二個索引數據顯示並保存在數據庫中時出現問題,出現如下錯誤:
“InvalidArgument = '1' 的值對 'index' 無效。參數名稱:index”

在此處輸入圖像描述

這是我的代碼

private void RunThis(object source, ElapsedEventArgs e)
        {
            //Console.WriteLine("Print this in every 10 seconds");
            this.Invoke(new MethodInvoker(delegate ()
            {
                int idwErrorCode = 0;

                string ip = txtIP.Text;
                string sdwEnrollNumber = "";
                int idwVerifyMode = 0;
                int idwInOutMode = 0;
                int idwYear = 0;
                int idwMonth = 0;
                int idwDay = 0;
                int idwHour = 0;
                int idwMinute = 0;
                int idwSecond = 0;
                int idwWorkcode = 0;

                int iGLCount = 0;
                int iIndex = 0;
                string iNoData = "NO DATA";
                string iError = "ERROR";
                string iDel = "DATA DELETE";
                

                Cursor = Cursors.WaitCursor;
                //lvLogs.Items.Clear();
                axCZKEM1.EnableDevice(iMachineNumber, false);//disable the device
                    if (axCZKEM1.ReadGeneralLogData(iMachineNumber))//read all the attendance records to the memory
                    {
                        while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out sdwEnrollNumber, out idwVerifyMode,
                                    out idwInOutMode, out idwYear, out idwMonth, out idwDay, out idwHour, out idwMinute, out idwSecond, ref idwWorkcode))//get records from the memory
                        {
                        if (iIndex >= 0)
                        {


                            lvLogs.Items.Clear();
                            iGLCount++;
                            lvLogs.Items.Add(iGLCount.ToString());
                            lvLogs.Items[iIndex].SubItems.Add(sdwEnrollNumber);//modify by Darcy on Nov.26 2009
                            lvLogs.Items[iIndex].SubItems.Add(ip.ToString());
                            lvLogs.Items[iIndex].SubItems.Add(idwVerifyMode.ToString());
                            lvLogs.Items[iIndex].SubItems.Add(idwInOutMode.ToString());
                            lvLogs.Items[iIndex].SubItems.Add(idwYear.ToString() + "-" + idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString());
                            lvLogs.Items[iIndex].SubItems.Add(idwWorkcode.ToString());
                            iIndex++;

                            try
                            {
                                //This is my connection string i have assigned the database file address path  
                                string MyConnection2 = "datasource=192.168.10.3;Port=3306;Database=Log_absen;Uid=manager;Pwd=@Gl0b4l1nd0";
                                //This is my update query in which i am taking input from the user through windows forms and update the record.  
                                //string Query = "insert into (dvc0005 set NIK='" + sEnrollNumber + "',Enroll='" + iYear + "' - '" + iMonth + "' - '" + iDay + "' where NIK='" + sEnrollNumber + "';)";

                                string NIK = sdwEnrollNumber;
                                string device = txtIP.Text.ToString();
                                string thn = idwYear.ToString();
                                string bln = (idwMonth < 10 ? '0' + idwMonth.ToString() : idwMonth.ToString());
                                string hr = (idwDay < 10 ? '0' + idwDay.ToString() : idwDay.ToString());
                                string jm = (idwHour < 10 ? '0' + idwHour.ToString() : idwHour.ToString()); ;
                                string mnt = (idwMinute < 10 ? '0' + idwMinute.ToString() : idwMinute.ToString()); ;
                                string dtk = (idwSecond < 10 ? '0' + idwSecond.ToString() : idwSecond.ToString()); ;
                                string tgl = thn + '-' + bln + '-' + hr + ' ' + jm + ':' + mnt + ':' + dtk;
                                string Query = "insert into logData_master(NIK,Enroll,deviceIP) value(@sEnrollNumber,@tgl,@ip)";

                                using (MySqlConnection conn = new MySqlConnection(MyConnection2))
                                {
                                    using (MySqlCommand cmd = new MySqlCommand(Query, conn))
                                    {
                                        cmd.Parameters.AddWithValue("@sEnrollNumber", NIK);
                                        cmd.Parameters.AddWithValue("@tgl", tgl);
                                        cmd.Parameters.AddWithValue("@ip", device);
                                        //cmd.Parameters.AddWithValue("@theText", theText);
                                        conn.Open();
                                        cmd.ExecuteNonQuery();

                                    }
                                }

                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }


                        }
                    }
                }
                else
                {
                    Cursor = Cursors.Default;
                    axCZKEM1.GetLastError(ref idwErrorCode);

                    if (idwErrorCode != 0)
                    {
                        lvLogs.Items.Clear();
                        lvLogs.Items.Add(iGLCount.ToString());
                        lvLogs.Items[iIndex].SubItems.Add(iError);
                        //MessageBox.Show("Reading data from terminal failed,ErrorCode: " + idwErrorCode.ToString(), "Error");
                    }
                    else
                    {
                        lvLogs.Items.Clear();
                        lvLogs.Items.Add(iGLCount.ToString());
                        lvLogs.Items[iIndex].SubItems.Add(iNoData);
                        //MessageBox.Show("No data from terminal returns!", "Error");
                    }
                }


                axCZKEM1.EnableDevice(iMachineNumber, false);//disable the device
                if (axCZKEM1.ClearGLog(iMachineNumber))
                {
                    lvLogs.Items.Clear();
                    axCZKEM1.RefreshData(iMachineNumber);//the data in the device should be refreshed
                    //MessageBox.Show("All att Logs have been cleared from teiminal!", "Success");
                    lvLogs.Items.Add(iGLCount.ToString());
                    lvLogs.Items[iIndex].SubItems.Add(iNoData);
                    
                }
                else
                {
                    lvLogs.Items.Clear();
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    lvLogs.Items.Add(iGLCount.ToString());
                    lvLogs.Items[iIndex].SubItems.Add(iError);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                }
                lvLogs.Items.Clear();
                axCZKEM1.EnableDevice(iMachineNumber, true);//enable the device
                Cursor = Cursors.Default;
            }));
        }

請幫我解決這個問題

我認為你的問題在這里:

lvLogs.Items.Clear();
iGLCount++;
lvLogs.Items.Add(iGLCount.ToString());
lvLogs.Items[iIndex].SubItems.Add(sdwEnrollNumber);

首先,您正在清除lvLogs.Items集合,這對於第一次來說很好。

第二次循環后,您將再次清除lvLogs.Items集合。

然后,您將 go 空集合。 使用lvLogs.Items.Add后,您的收藏將包含一項。

但是,由於您的集合只有一項並且iIndex變量的值為1 ,因此您嘗試使用不存在的lvLogs.Items[iIndex]訪問第二項。

這純粹基於對您的源代碼的假設。

如果不探索所有 class 結構,就無法說出更多或如何修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM