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?