Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 30-Aug-11 21:52pm
Edited 31-Aug-11 2:14am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
biswarup88 at 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!...");
}
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,205
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,474


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 31 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100