Solved

Loop Problem (Object Reference Problem)

Posted on 2003-12-08
6
421 Views
Last Modified: 2010-04-16
Hi.
My problem is simple, that's why i think the soulution won't be...

I Have this loop:

for( int y=0; y<oDS.Tables["Databases"].Rows.Count; y++ )
{
      oLI = new ListItem( oDS.Tables["Databases"].Rows[y]["TableCatalog"].ToString(), oDS.Tables["Databases"].Rows[y]["TableCatalog"].ToString() );
      dropDatabase.Items.Add( oLI );
}

It gave me a object reference error. But the quick watch shows "oDS.Tables["Databases"].Rows[y]["TableCatalog"]" with the prober value when I run the DEBUG.

Then I solved it with this:

string strTemp;

for( int y=0; y<oDS.Tables["Databases"].Rows.Count; y++ )
{
      strTemp = oDS.Tables["Databases"].Rows[y]["TableCatalog"].ToString();
      oLI = new ListItem( strTemp , strTemp );
      dropDatabase.Items.Add( oLI );
}

Why the hell the first one didn't worked out?

Thanks!
Roberto Colnaghi Junior
0
Comment
Question by:3DGames
6 Comments
 
LVL 18

Expert Comment

by:testn
Comment Utility
Is tablecatalog field ntext or binary?
0
 

Author Comment

by:3DGames
Comment Utility
It is a VARCHAR(64) SQL SERVER 2000 datatype.
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
Comment Utility
An interesting problem and it was probably not in the oDS.Tables["Databases"].Rows[y]["TableCatalog"] but in some other point...

Never mind, I would suggest you to change your code to something easier to read like:

foreach (DataRow row in oDS.Tables["Databases"].Rows)
{
     oLI = new ListItem(row["TableCatalog"].ToString(), row["TableCatalog"].ToString() );
     dropDatabase.Items.Add( oLI );
}

Try it then and see what happens
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
it would actually be BETTER (more efficient) to use somethig like this:

Sting  strRow as String;
foreach (DataRow row in oDS.Tables["Databases"].Rows)
{
     strRow = row["TableCatalog"].ToString();
     oLI = new ListItem(strRow,strRow);
     dropDatabase.Items.Add( oLI );
}

this makes a SINGLE call to the row["TableCatalog"].ToString() method, and 'caches' the result as strRow, which can then be used twice, rather than requiring the toString method to be executed twice.  This will be more efficient, performance wise.

AW

0
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
Arthur_Wood: 3DGames already made this but he had a problem when the calls to ToString were two (have a look at the above posts) so I suggested that he calls the ToString method again twice to see if there is any difference
0
 

Author Comment

by:3DGames
Comment Utility
Well... Thanks for the solution!

But the question remains.... why my first loop didn't worked?

Your answer(TheAvenger) are almost the same as it...... Ok, it is much more clean and easier to read :) , but I think that both should work.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now