LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Index was out of range. Must be non-negative and less than the size of the collectio (http://www.linuxquestions.org/questions/programming-9/index-was-out-of-range-must-be-non-negative-and-less-than-the-size-of-the-collectio-425049/)

mrobertson 03-15-2006 08:21 AM

Index was out of range. Must be non-negative and less than the size of the collectio
 
I am using a custom controls datagrid in a visual C# application. I continually got the following error when I click a button to run a query:

Index was out of range. Must be non-negative and less than the size of the collection. Her is the code that I am using to populate the datagrid:
Code:

private void DateTimePicker2_ValueChanged(object sender, System.EventArgs e)
                {
                        //Sets second date to query between
                        string Selecteddate2 = DateTimePicker2.Value.Date.ToString("MM-dd-yyyy");
                }

                public void PerformQueryIntoDataSet()
                {
                        try
                        {
                                //Declare an oracle connection
                                System.Data.Odbc.OdbcConnection ODBCConnect = new System.Data.Odbc.OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=duferco.world;Uid=pcc;Pwd=duferco");
                               
                                string Selecteddate = DateTimePicker1.Value.Date.ToString("MM-dd-yyyy");
                                string Selecteddate2 = DateTimePicker2.Value.Date.ToString("MM-dd-yyyy");

                                if (ckbx28Mill.Checked && ckbx35Mill.Checked)
                                {
                                        MessageBox.Show ("Please select either 28 Mill or 35 mill.");
                                }
                                else if(ckbx35Mill.Checked)
                                {                                        string Querystring = "SELECT ID FROM reported_coil_35mill@L3 where ACTUAL_DATE between TO_DATE('" + Selecteddate + ", " + cmbtime1.Text + "', 'MM-DD-YYYY, HH24:mi:SS') and TO_DATE('" + Selecteddate2 + ", " + cmbtime2.Text + "', 'MM-DD-YYYY, HH24:mi:SS')";
                                       
                                        System.Data.Odbc.OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(Querystring, ODBCConnect);
                                        DataSet1 = new DataSet("Reported Coil Data");
                               
                                        Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                               
                                        //Check to see if a connection is open and if not open one
                                        if (ODBCConnect.State == ConnectionState.Open)
                                        {
                                                //Close and re-open a connection
                                                ODBCConnect.Close();
                                                ODBCConnect.Open();
                                        }
                                        else
                                        {
                                                //open connection
                                                ODBCConnect.Open();
                                        }
                                        //Declare and Define data adapter
                                        System.Data.Odbc.OdbcDataAdapter da = new System.Data.Odbc.OdbcDataAdapter(Querystring, ODBCConnect);
                                       
                                        System.Data.DataTable Table = new System.Data.DataTable("Level_3");

                                        //Fill the DataAdapter
                                        da.Fill(DataSet1, "Level_3");

                                        //Close the Connection to Oracle
                                        ODBCConnect.Close();
                               
                                        datagrid1.DataSource = DataSet1.Tables[0];
                                        //datagrid1.NavigateTo(0, System.Convert.ToString(- 1));
                                        //datagrid1.NavigateTo(0, "Level_3");
                                        //dataGrid.PreferredColumnWidth = 200;
                                        Cursor.Current = System.Windows.Forms.Cursors.Arrow;
                                }

The code is not erroring out in this sub however it errors at the end of:
Code:

static void Main()
                {
                        Application.Run(new Form1());
                }

Does anyone know a fix or cause for this or see where the problem may be in my code?

FLLinux 03-15-2006 10:42 AM

Just a quick look at it, and not having all the code, I would check the DataSet1.Tables[0] call to make sure it is not returning NULL. Also you might look into pulling the Tables from the DataSet out into a DataTableCollection and then assign the tables into the datagrid from there.

Just my thoughts.

mrobertson 03-15-2006 10:47 AM

I know that the dataset is not null be cause when I run the query, the datagrid populates and a second or 2 later the error occurs. How do you pull the tables into a DataTableCollection like you mention?


All times are GMT -5. The time now is 02:19 AM.