Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
Posted
Updated 31-Aug-11 1:14am
v2

You can't select a cell on a sheet that is not the active sheet.

Just as a note, you should be able to debug and get to the line that is throwing this error.
You have a provided a lot of code in your snippet, but it is close to impossible to go through every line without actually debugging.
 
Share this answer
 
Comments
sahabiswarup 31-Aug-11 3:36am    
But when i am debugging this following code i dn't get such error message.

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);
xlWorkSheet = (ex.Worksheet)xlWorkBook.Worksheets.get_Item(1);
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!...");
}
}
If there is not a really good reason to be using Excel as a primary data source, you should use a database. Excel isn't designed to be one, and requiring the user to have Office (a non free product) to run your app seems silly when you could use a free database (mySQL, SQL Server Express, etc).

My guess is that the row you are trying to select doesn't have any data so Excel thinks it's outside the available worksheet (possibly because you are looking in a worksheet that isn't the one you meant to). But it is pretty much impossible to debug code like that from outside.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900