Help regarding deletion first file and then importing file

  • Thread starter Thread starter NK sharma
  • Start date Start date
N

NK sharma

Guest
I am getting the following exception when i delete the existing file and import another.

$Exception {"Violation of concurrency: DeleteCommand affected 0 of the expected 1 records."} System.Exception {} System.Data.DBConcurrencyException

I use two functions one to delete the first file and then import another one

the function which i used to delete is as follow.

public void Clear()
/*
* This method clears the current section by deleting it in DB, and recreating it.
*/
{
TroboDataSet.SectionRow currentSectionRow = DataManager.Instance.TroboDataSet.Section.FindBySectionID(this.SectionID);

//Save the address
try
{
this.Address = new Address(currentSectionRow.AddressID);
}
catch (System.Data.StrongTypingException)
{
//Case where the section doesnt have an address.
}
DataManager.Instance.DeleteSection(currentSectionRow);
this.SectionID = 0;
this.SectionID = DataManager.Instance.CurrentSchool.AddSection(this);

then i call the function to import excel file where i use the following function

public ErrorItem ProcessExcel(string filePath, Section section, bool itIsXLSX)
{


ErrorItem errorItem = new ErrorItem("Erreur lors de limportation du fichier Excel pour la section " + section.Name + " . Vérifiez que les lignes respectent le format Classe, Nom, Prénom.");
int lineCounter = 0;
//int currentLine;

//TODO: Gérer les lignes vides et exceptions.

//TODO: Set delimiter characters in the setting dialog
try
{
//Create teacher class
cClass teacherClass = new cClass("ENSEIGNANTS", section.SectionID);
cClass ClassNameLessClass = new cClass("SANS_CLASSE", section.SectionID);
DataManager.Instance.Update(teacherClass);
DataManager.Instance.Update(ClassNameLessClass);
string strConn;
if(firstRowIsTitle)
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=NO;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";


using (OleDbConnection oleDB = new OleDbConnection(strConn))
{

DataSet DS = new DataSet();
string[] items = new string[10];

oleDB.Open();

// Get the name of the first worksheet:
DataTable dbSchema = oleDB.GetOleDbSchemaTable (OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
throw new Exception ("Error: Could not determine the name of the first worksheet.");
}
string firstSheetName = dbSchema.Rows [0] ["TABLE_NAME"].ToString ();

// Now we have the table name; proceed as before:
OleDbCommand dbCommand = new OleDbCommand ("SELECT * FROM [" + firstSheetName + "]", oleDB);

//OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Feuille1$]", oleDB);

DataTable dataTable = new DataTable();
OleDbDataReader reader = dbCommand.ExecuteReader();

//while(reader.Read())
dataTable.Load(reader);
int noOfColumns= dataTable.Columns.Count;
foreach (DataRow row in dataTable.Rows)
{
//Array.Clear(items, 0, items.Length);
//lineCounter++;
string []currentLine ;
for(int i=0; i<6;i++)
{
if (noOfColumns <i+1)
items = "";
else
items= (row.ItemArray.ToString());

}

currentLine = items;
string messageForError = "";
int columnsNos = dataTable.Rows.Count;
if (columnsNos < 3 || items[dictionaryForCSVFormat["Classe"]] == "" || items[dictionaryForCSVFormat["Nom"]] == "" || items[dictionaryForCSVFormat["Prénom"]] == "")
{
if (items[dictionaryForCSVFormat["Nom"]] == "")
{
messageForError = "-> Nom manquant. Il est nommé comme: SANS_NOM";
items[dictionaryForCSVFormat["Nom"]] = "SANS_NOM";
}
if (items[dictionaryForCSVFormat["Classe"]] == "")
{
messageForError = "-> Nom de classe manquant. Il est nommé comme: SANS_CLASSE";
items[dictionaryForCSVFormat["Classe"]] = "SANS_CLASSE";
}
if (items[dictionaryForCSVFormat["Prénom"]] == "")
{
messageForError = "-> Prénom manquant. Il est nommé comme:Sans_Prénom";
items[dictionaryForCSVFormat["Prénom"]] = "Son_Prénom";
}
string error = "\n\n Erreur à la ligne " + lineCounter + " du fichier :Excel " +currentLine+ messageForError +"\n\n";
errorItem.ErrorQueue.Enqueue(error);
}
else
saveValuesInDB(items, section,lineCounter);


}
ProcessLastNameCounter(section);
if (haveWorkersBeenImportedFlag)
{
System.Windows.Forms.MessageBox.Show("ATTENTION: Des personnes ont été importées dans la classe ENSEIGNANTS.\n" +
"Elles sont considérées comme faisant partie du personnel de lécole, et auront droit à des produits gratuits.\n" +
"Si vous pensez que ces personnes ne font pas partie du personnel, veuillez les déplacer hors de la classe ENSEIGNANTS", "Avertissement");
}
return errorItem;

}

}
catch (Exception ex)
{

throw ex;
}
}


here ESINGNANT and SANS_CLASSE are the commen in every class so when i import new file then i create these two classes.

please help me regarding this.



Thank you

Continue reading...
 
Back
Top