LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2006, 08:21 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
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?
 
Old 03-15-2006, 10:42 AM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
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.
 
Old 03-15-2006, 10:47 AM   #3
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing icon theme? i have a index.theme and index.desktop, no directions! mr_coffee Linux - Newbie 2 01-25-2006 01:45 PM
printing negative value as positive hubabuba Programming 5 05-03-2005 12:41 PM
df reporting negative disk size??? rimcrazy Linux - Hardware 2 09-07-2004 10:08 AM
/dev/hda2 negative size? yozd Linux - Hardware 4 07-24-2004 08:41 PM
Negative display for X? unholy Linux - General 0 11-15-2003 08:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration