I have written this code to delete row from Excel database but when i am running this i get the above error. Please help to to solve this.
Here is my code:
if (MessageBox.Show("Do you realy want to delete this data?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (index != -1)
{
ex.Application xlApp;
ex.Workbook xlWorkBook;
ex.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new ex.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(Path.GetDirectoryName(Application.ExecutablePath) + "\\Taas SMS.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
if (TaasSMS.AppCode.GenModule.Type == "Member")
{
xlWorkSheet = (ex.Worksheet)xlWorkBook.Worksheets.get_Item(2);
ex.Range range;
range = (ex.Range)xlWorkSheet.Rows[index + 2, Type.Missing];
range.Select();
range.Delete(ex.XlDirection.xlUp);
xlWorkBook.SaveAs(Path.GetDirectoryName(Application.ExecutablePath) + "\\Temp\\Taas SMS.xls", ex.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ex.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
else if (TaasSMS.AppCode.GenModule.Type == "Media")
{
xlWorkSheet = (ex.Worksheet)xlWorkBook.Worksheets.get_Item(3);
ex.Range range;
range = (ex.Range)xlWorkSheet.Rows[index + 2, Type.Missing];
range.Select();
range.Delete(ex.XlDirection.xlUp);
xlWorkBook.SaveAs(Path.GetDirectoryName(Application.ExecutablePath) + "\\Temp\\Taas SMS.xls", ex.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ex.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
index = -1;
string sourcePath2 = Path.GetDirectoryName(Application.ExecutablePath) + "\\Temp\\Taas SMS.xls";
string DestinationPath2 = Path.GetDirectoryName(Application.ExecutablePath) + "\\Taas SMS.xls";
File.Delete(DestinationPath2);
File.Move(sourcePath2, DestinationPath2);
FillGrid();
MessageBox.Show("Data Deleted!...");
}
at
range.Select();
i get that above error: COM exception was unhandled (Select method of Range class failed)