Link to home
Start Free TrialLog in
Avatar of rciner313
rciner313Flag for United States of America

asked on

C# checkedlistbox.items.add problem

I am trying to have a checklistbox populate from certain criteria out of an excel spread sheet. When the code reaches the Microsoft.Office.Interop.Excel.Application xl portion of the code. The Form disappears and then reappears but the code will not run past this point. What am I doing wrong?


********************************************************************************************************************
int cntr = 2;
            Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = xl.Workbooks.Open(@"C:\Users\rciner\Documents\GROUP MUSTER.xls", 0, false, false, false, false, true, Missing.Value, Missing.Value, true, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
           
            xl.Visible = true;
            Worksheet ws = (Worksheet)wb.Worksheets[1];
            Range aRange = ws.get_Range("a" + Convert.ToString(cntr), Missing.Value);
            Range lRange = ws.get_Range("l" + Convert.ToString(cntr), Missing.Value);

            checkedListBox1.Items.Add("ciner");
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

Have you put a breakpoint and checked values of xl, wb, ws...
Avatar of rciner313

ASKER

I have put a break point in and i got the error : A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in MOB_Traning_Data.exe

OK - so that is why it stops.

Do you have Excel installed on this PC ?
when i press F5 to continue the program past the break point it does not respond. I have excel on this PC and I have used this method for comboboxes and it works there, just not with the checklistbox.items.add portion.
Exactly which line results in this error you get ?
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
>>I have used this method for comboboxes

In this application?
Check if you do something different in code prior to the above line (I assume you do use the same line of code)
I have typed it out and copied and pasted it, nothing seems to work. Here is a samples from a place where it works. I am wondering if there is a glitch with the private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) that causes the freeze with a checked list box:

int cntr=0;
            char col = 'a';
            int colcntr = 0;
            int lccntr = 1;
            cntr = comboBox2.SelectedIndex + 2;
            colcntr = comboBox1.SelectedIndex + 1;
            Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
I've never tried to automate excel (or other COM based application) from a managed (eg. C#) application.  From unmanaged code (C++) one needed to 'start' support of COM via a fuction call BEFORE any attempt to use it was made.  Typically this would be done at application start (and the shutdown of COM support was performed when the app was stopped).

I'll see if I have time next week to make a small automation of excel attempt from C# code.
ASKER CERTIFIED SOLUTION
Avatar of rciner313
rciner313
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I reloaded windows 7 and c# and now the problem is solved