Error loading datagridview...

cboshdave

Member
Joined
Dec 11, 2014
Messages
24
Programming Experience
1-3
I have seen several postings on this error, but I can't seem to make the answers fit my problem.
Generally, it is a simple dgv load with a simple query. The error I get is:
Error: System.ArgumentOutOfRangeException: Index was out of range. Here is the code:


C#:
string sSQL =
"select * from Table ORDER BY FieldFK"; 
DataClass dbName = new DataClass(sDBConnection);
dbName.OpenConnection();
//Load the data to the screen.
SqlDataAdapter sda  = dbName.ExecuteAdapter(sSQL); //
DataSet ds = new DataSet();
sda.Fill(ds);
bsSQLData.DataSource = ds.Tables[0];
dgvSQL.DataSource = bsSQLData;

The first time thru the code, it throws the error on "dgvSQL.DataSource = bsSQLData"
The subsequent times thru the same code, it fails on "bsSQLData.DataSource = ds.Tables[0]" and throws the error twice.
I am baffled! I have tried several different ways to load the grid. Same result.
The grid DOES still load, but with the error that must be acknowledged.
My grid is set to readonly. I just did not think there was anything fancy here!


I would appreciate any thoughts.
 
When an exception is thrown, the IDE gives you a fair bit of information. Have you looked at it? One of the most important pieces of information is the stack trace. It shows all the function calls active at the time, so it pinpoints exactly where the exception was thrown and exactly how your code got there. At a glance, it seems like indexing the Tables collection of the DataSet might be the issue but a closer look makes that seem unlikely. The stack trace will tell you either way. It may be something funky going on in the grid itself due to some corruption.

Regardless of anything else, why create a DataSet when all you intend to do is use a DataTable? If you want a DataTable then create a DataTable. That way, you'll know for sure that indexing the Tables collection of a DataSet is not the issue.
 
Back
Top Bottom