My code works well when I'm adding less than 30 rows. But It can't handle more than that. How can I overcome this?
I get this error:Unspecified error when I'm trying to add less than 30, all the rows are added. and when I'm trying to add more than 30, it doesn't add nothing, and I get the error the number of the rows times.
Here is the code:
for (int i = 0; i < st1.Length; i++)
{
UpdateDataBase(st1[i]);
}
private void UpdateDataBase(char letter)
{
letter = char.ToUpper(letter);
int serialPro = 0;
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
OleDbConnection connection = new OleDbConnection(connectionString);
string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
OleDbCommand command = new OleDbCommand(sql, connection);
try
{
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
//get the last!
while (reader.Read())
serialPro = reader.GetInt32(reader.Depth);
sql = "INSERT INTO tblOrderAA (orderAASerialPro, orderAACodon1) "
+ " values (?, ?)";
OleDbCommand command2 = new OleDbCommand(sql, connection);
command2.CommandType = CommandType.Text;
command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
command2.Parameters.AddWithValue("orderAACodon1", letter);
command2.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
this.Close();
}
}`
EDIT: private void UpdateDataBase(char letter) { letter = char.ToUpper(letter); int serialPro = 0; string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=C:\Projects_2012\Project_Noam\Access\myProject.accdb";
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataReader reader = command.ExecuteReader())
{
//get the last!
while (reader.Read())
serialPro = reader.GetInt32(0);
}
sql = "INSERT INTO tblOrderAA (orderAASerialPro, orderAACodon1) "
+ " values (?, ?)";
using (OleDbCommand command2 = new OleDbCommand(sql, connection))
{
command2.CommandType = CommandType.Text;
command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
command2.Parameters.AddWithValue("orderAACodon1", letter);
command2.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
}
}
enter code here