Solved

Loop Problem (Object Reference Problem)

Posted on 2003-12-08
6
427 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
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!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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