We help IT Professionals succeed at work.

I am wrting a C# Windows app in VS2005 and do you know how would I resolve the error _COMPlusExceptionCode -532459699  "IndexOutOfRangeException was unhandled. Cannot find table 0"?

zimmer9
zimmer9 used Ask the Experts™
on
I am developing a C# Windows application using VS2005. I use SQL Server 2005 as the back end database. Do you know how I can resolve the error:

"IndexOutOfRangeException was unhandled. Cannot find table 0."
_COMPlusExceptionCode -532459699

The offending statement is at the following line of code at the beginning of my FOR loop:  
i <= ds.Tables[0].Rows.Count - 1

This error occurs in my module titled FallQueryACSToExcel which I attached.
FallQueryACSToExcel.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You should check your query to make sure it is returning the right information.  The call to adapter.fill is not gathering any data into the dataset in this case.

If ds.tables[0] is null (empty, meaning there are no tables at all!) then you would get the error you are seeing.  So...first I would try sending in a simple query rather than using the stored procedure.  like "Select Top 5 * from table" or whatever.  Then test your code and make sure it works.  If so, it is likely an error with the stored procedure.  Perhaps some syntax is wrong, or you are passing the wrong parameters.

But regardless of WHAT the issue causing this error is...the error means you have no tables in the dataset (or at least not one at index 0)  ;-)

Author

Commented:
I tried running the following code and it works fine. I just substitued a query in place of the original stored procedure. My original code is in the attachment titled FallQueryACSToExcel.txt

private void FallQueryACSToExcel(string reportyr)
        {
            int i = 0;
            int j = 0;
            string data = null;
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
 
            SqlConnection _conn = new System.Data.SqlClient.SqlConnection("Data Source=F,3;Initial Catalog=L;User ID =L;PWD=C;");
            _conn.Open();
           
            SqlCommand command = new SqlCommand("SELECT * FROM tblFlACS", _conn);
           
            command.CommandType = CommandType.Text;
           
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "tblFlACS");                    
           
            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
Commented:
Great, so that means there is either an error in your stored proc, or else you are passing something into the stored proc wrong.

Here is a simple example of how to call a stored proc with a dataadapter...pretty similar to what you have done, so there may be an issue with your stored proc.

http://stackoverflow.com/questions/3528305/how-to-use-a-dataadapter-with-stored-procedure-and-parameter

That may be a question better posted in another thread...you can resolve this one if we've helped you enough or post the stored proc here (though without your data it would be tricky to diagnose I guess).