Reputation: 93
regards,
I have a function to pull data from the attendance machine to be displayed and stored in the database and then delete the data from the machine ,,
Then, I have a problem when the 2nd index data will be displayed and saved in the database, an error appears like this:
"InvalidArgument = Value of '1' is not valid for 'index'. Parameter name: index"
this is my code :
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;
}));
}
please help me to solve this
Upvotes: 0
Views: 389
Reputation: 2723
I think your problem is here:
lvLogs.Items.Clear();
iGLCount++;
lvLogs.Items.Add(iGLCount.ToString());
lvLogs.Items[iIndex].SubItems.Add(sdwEnrollNumber);
First, you are clearing lvLogs.Items
collection, which is fine for the first time.
After it loops for the second time, you are again clearing the lvLogs.Items
collection.
Then, you will go empty collection. After using lvLogs.Items.Add
your collection will contain one item.
But, since your collection has only one item and iIndex
variable's value is 1
, you are trying to access the second item with lvLogs.Items[iIndex]
which doesn't exist.
This is purely based on assumptions with your source code.
Can't tell more or how to fix it without exploring all of your class structures.
Upvotes: 1