Solved

Loop Problem (Object Reference Problem)

Posted on 2003-12-08
6
430 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
[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
6 Comments
 
LVL 18

Expert Comment

by:testn
ID: 9898623
Is tablecatalog field ntext or binary?
0
 

Author Comment

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

Accepted Solution

by:
TheAvenger earned 125 total points
ID: 9899972
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
Industry Leaders: 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!

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9904883
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
ID: 9904927
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
ID: 9911291
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

Independent Software Vendors: 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

705 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