ironwill96
asked on
Using joins with a C# DataReader - getting IndexOutOfRangeExceptions
Hi all,
I am having some issues with trying to retrieve the results of a Join using a DataReader. The syntax seems to work ok as a record is returned but I want to access one column from the table i'm joining to and it won't let me for some reason.
Here is the relevant code (I have commented out sections to just try to debug the one column).
try
{
//this.mstrSQL = "SELECT ProjectID FROM Projects WHERE (ProjectID =@@IDENTITY)";
this.mstrSQL = "SELECT Customers.CompanyName FROM Customers, Projects WHERE ProjectID = " + this.mintProjectID + " AND Projects.CustomerID=Custom ers.Custom erID";
//this.mstrSQL = "SELECT * FROM Projects, Customers WHERE ProjectID = " + this.mintProjectID + " AND Projects.CustomerID=Custom ers.Custom erID";
this.openDatabase();
OleDbCommand myCmd = new OleDbCommand(this.mstrSQL, this.dbConnect);
OleDbDataReader myReader = myCmd.ExecuteReader();
if (myReader.HasRows)
{
while (myReader.Read())
{
//we found a value, grab the ProjectID
/*
this.txtProjectTitle.Text = (String)myReader["ProjectT itle"];
this.txtProjectNotes.Text = (String)myReader["ProjectN otes"];
this.txtStartDate.Text = System.Convert.ToString(my Reader["St artDate"]) ;
this.txtDueDate.Text = System.Convert.ToString(my Reader["Du eDate"]);
this.mintProjectID = (int)myReader["ProjectID"] ;
this.txtRender1Loc.Text = System.Convert.ToString(my Reader["Re nderLocati on1"]);
this.txtRender1Desc.Text = System.Convert.ToString(my Reader["Re nderDescri ption1"]);
this.txtRender2Loc.Text = System.Convert.ToString(my Reader["Re nderLocati on2"]);
this.txtRender2Desc.Text = System.Convert.ToString(my Reader["Re nderDescri ption2"]);
this.txtRender3Loc.Text = System.Convert.ToString(my Reader["Re nderLocati on3"]);
this.txtRender3Desc.Text = System.Convert.ToString(my Reader["Re nderDescri ption3"]);
this.txtRender4Loc.Text = System.Convert.ToString(my Reader["Re nderLocati on4"]);
this.txtRender4Desc.Text = System.Convert.ToString(my Reader["Re nderDescri ption4"]);
this.txtRender5Loc.Text = System.Convert.ToString(my Reader["Re nderLocati on5"]);
this.txtRender5Desc.Text = System.Convert.ToString(my Reader["Re nderDescri ption5"]);
*/
this.cboCustomers.Text = System.Convert.ToString(my Reader["Cu stomers.Co mpanyName" ]); //LINE I AM THROWING EXCEPTION ON
break;
}
}
else
{
//we didnt find any values, this is an issue, abort
MessageBox.Show("Unable to locate any valid data in this project!", "Data Value Error");
myReader.Close();
return;
}
myReader.Close();
this.closeDatabase();
}
catch (Exception exp)
{
//something went wrong, abort
string tempmsg = exp.Message;
string tempmsg2 = exp.ToString(); //<-- This shows that it is an IndexOutOfRangeException on "Customers.CompanyName"
this.closeDatabase();
//MessageBox.Show("Excepti on thrown during attempt to obtain ProjectID, aborting.", "Fatal Data Error");
}
I know my two SQL statements are a bit different, but neither one works and switching the ordering of Customers, Projects to Projects,Customers seems to make no difference either. It is possible what I am doing is no longer allowed as the way I am doing the Join doesnt use the JOIN keyword itself and worked great under ADO and DAO back in my VB6 days but perhaps I need some different syntax to accomplish the same thing now.
Thanks,
Nathan
I am having some issues with trying to retrieve the results of a Join using a DataReader. The syntax seems to work ok as a record is returned but I want to access one column from the table i'm joining to and it won't let me for some reason.
Here is the relevant code (I have commented out sections to just try to debug the one column).
try
{
//this.mstrSQL = "SELECT ProjectID FROM Projects WHERE (ProjectID =@@IDENTITY)";
this.mstrSQL = "SELECT Customers.CompanyName FROM Customers, Projects WHERE ProjectID = " + this.mintProjectID + " AND Projects.CustomerID=Custom
//this.mstrSQL = "SELECT * FROM Projects, Customers WHERE ProjectID = " + this.mintProjectID + " AND Projects.CustomerID=Custom
this.openDatabase();
OleDbCommand myCmd = new OleDbCommand(this.mstrSQL,
OleDbDataReader myReader = myCmd.ExecuteReader();
if (myReader.HasRows)
{
while (myReader.Read())
{
//we found a value, grab the ProjectID
/*
this.txtProjectTitle.Text = (String)myReader["ProjectT
this.txtProjectNotes.Text = (String)myReader["ProjectN
this.txtStartDate.Text = System.Convert.ToString(my
this.txtDueDate.Text = System.Convert.ToString(my
this.mintProjectID = (int)myReader["ProjectID"]
this.txtRender1Loc.Text = System.Convert.ToString(my
this.txtRender1Desc.Text = System.Convert.ToString(my
this.txtRender2Loc.Text = System.Convert.ToString(my
this.txtRender2Desc.Text = System.Convert.ToString(my
this.txtRender3Loc.Text = System.Convert.ToString(my
this.txtRender3Desc.Text = System.Convert.ToString(my
this.txtRender4Loc.Text = System.Convert.ToString(my
this.txtRender4Desc.Text = System.Convert.ToString(my
this.txtRender5Loc.Text = System.Convert.ToString(my
this.txtRender5Desc.Text = System.Convert.ToString(my
*/
this.cboCustomers.Text = System.Convert.ToString(my
break;
}
}
else
{
//we didnt find any values, this is an issue, abort
MessageBox.Show("Unable to locate any valid data in this project!", "Data Value Error");
myReader.Close();
return;
}
myReader.Close();
this.closeDatabase();
}
catch (Exception exp)
{
//something went wrong, abort
string tempmsg = exp.Message;
string tempmsg2 = exp.ToString(); //<-- This shows that it is an IndexOutOfRangeException on "Customers.CompanyName"
this.closeDatabase();
//MessageBox.Show("Excepti
}
I know my two SQL statements are a bit different, but neither one works and switching the ordering of Customers, Projects to Projects,Customers seems to make no difference either. It is possible what I am doing is no longer allowed as the way I am doing the Join doesnt use the JOIN keyword itself and worked great under ADO and DAO back in my VB6 days but perhaps I need some different syntax to accomplish the same thing now.
Thanks,
Nathan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bah, you'd think I would have tried dropping the table prefix. Thanks angelIII!
Nathan
Nathan
do
{
while (myReader.Read())
{
}
}
while(myReader.NextResult(
}