[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using joins with a C# DataReader - getting IndexOutOfRangeExceptions

Posted on 2006-06-25
3
Medium Priority
?
391 Views
Last Modified: 2008-03-10
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=Customers.CustomerID";
                //this.mstrSQL = "SELECT * FROM Projects, Customers WHERE ProjectID = " + this.mintProjectID + " AND Projects.CustomerID=Customers.CustomerID";
                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["ProjectTitle"];                        
                        this.txtProjectNotes.Text = (String)myReader["ProjectNotes"];
                        this.txtStartDate.Text = System.Convert.ToString(myReader["StartDate"]);
                        this.txtDueDate.Text = System.Convert.ToString(myReader["DueDate"]);
                        this.mintProjectID = (int)myReader["ProjectID"];                        
                        this.txtRender1Loc.Text = System.Convert.ToString(myReader["RenderLocation1"]);
                        this.txtRender1Desc.Text = System.Convert.ToString(myReader["RenderDescription1"]);
                        this.txtRender2Loc.Text = System.Convert.ToString(myReader["RenderLocation2"]);
                        this.txtRender2Desc.Text = System.Convert.ToString(myReader["RenderDescription2"]);
                        this.txtRender3Loc.Text = System.Convert.ToString(myReader["RenderLocation3"]);
                        this.txtRender3Desc.Text = System.Convert.ToString(myReader["RenderDescription3"]);
                        this.txtRender4Loc.Text = System.Convert.ToString(myReader["RenderLocation4"]);
                        this.txtRender4Desc.Text = System.Convert.ToString(myReader["RenderDescription4"]);
                        this.txtRender5Loc.Text = System.Convert.ToString(myReader["RenderLocation5"]);
                        this.txtRender5Desc.Text = System.Convert.ToString(myReader["RenderDescription5"]);                        
                        */
                        this.cboCustomers.Text = System.Convert.ToString(myReader["Customers.CompanyName"]);  //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("Exception 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
0
Comment
Question by:ironwill96
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16979920
don't use the tablename, only the column name:
                        this.cboCustomers.Text = System.Convert.ToString(myReader["CompanyName"]);  

if you find the same column name in both tables, you have to give the column an alias in the query.
anyhow, you should avoid using "SELECT * ", but specify the column list of only those columns that you really need.
0
 

Expert Comment

by:mattbelfast23
ID: 16993332
The code you have shown looks like its doing 3 separate queries which would return 3 separate result sets.  You need to use myReader.NextResult() to advance to the next available result set.

do
{
                    while (myReader.Read())
                    {
                    }
}
while(myReader.NextResult())
}
0
 
LVL 2

Author Comment

by:ironwill96
ID: 16995870
Bah, you'd think I would have tried dropping the table prefix.  Thanks angelIII!

Nathan
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question