Solved

Loop Problem (Object Reference Problem)

Posted on 2003-12-08
6
426 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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