Try 'i < dTable.Rows.Count' instead of 'i <= dTable.Rows.Count'.
Also try 'object value = dTable.Rows[i][2]' without .ToString().
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello to everyone,
I'm saving all rows in a specific column in an Excel table to the database. There is no problem, all the data in the column of the table I want is transferred to the database. However, when it comes to the last row, I encounter the error 'there is now at position'. However, when I checked it with the debugger, the data in the row was pulled. So even though the data is being pulled, not null, I'm getting this error. Strangely enough, the data on the row where I got the error is saved to the database without any problems. What is the solution to the problem?
Excel;
Database;
As you can see the last line has been printed to the database. However, I am getting an error.
C#
foreach (DataRow drow in dTable.Rows)
{
for (int i = 0; i <= dTable.Rows.Count; i++)
{
object value = dTable.Rows[i][2].ToString();
if (value != DBNull.Value)
{
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO aList(sN) VALUES(@sN)", conn))
{
cmd.Parameters.AddWithValue("@sN", dTable.Rows[i][2].ToString());
int check = cmd.ExecuteNonQuery();
conn.Close();
}
}
}
else { }
}
}
DataTable row count;
Row 7 actually exists and the data in it has been pulled;
Try 'i < dTable.Rows.Count' instead of 'i <= dTable.Rows.Count'.
Also try 'object value = dTable.Rows[i][2]' without .ToString().
Here is the solution;
foreach (DataRow drow in dTable.Rows)
{
var value = drow[2].ToString();
}