3DGames
asked on
Loop Problem (Object Reference Problem)
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"].Ro ws[y]["Tab leCatalog" ].ToString (), oDS.Tables["Databases"].Ro ws[y]["Tab leCatalog" ].ToString () );
dropDatabase.Items.Add( oLI );
}
It gave me a object reference error. But the quick watch shows "oDS.Tables["Databases"].R ows[y]["Ta bleCatalog "]" 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"].Ro ws[y]["Tab leCatalog" ].ToString ();
oLI = new ListItem( strTemp , strTemp );
dropDatabase.Items.Add( oLI );
}
Why the hell the first one didn't worked out?
Thanks!
Roberto Colnaghi Junior
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"].
{
oLI = new ListItem( oDS.Tables["Databases"].Ro
dropDatabase.Items.Add( oLI );
}
It gave me a object reference error. But the quick watch shows "oDS.Tables["Databases"].R
Then I solved it with this:
string strTemp;
for( int y=0; y<oDS.Tables["Databases"].
{
strTemp = oDS.Tables["Databases"].Ro
oLI = new ListItem( strTemp , strTemp );
dropDatabase.Items.Add( oLI );
}
Why the hell the first one didn't worked out?
Thanks!
Roberto Colnaghi Junior
Is tablecatalog field ntext or binary?
ASKER
It is a VARCHAR(64) SQL SERVER 2000 datatype.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it would actually be BETTER (more efficient) to use somethig like this:
Sting strRow as String;
foreach (DataRow row in oDS.Tables["Databases"].Ro ws)
{
strRow = row["TableCatalog"].ToStri ng();
oLI = new ListItem(strRow,strRow);
dropDatabase.Items.Add( oLI );
}
this makes a SINGLE call to the row["TableCatalog"].ToStri ng() 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
Sting strRow as String;
foreach (DataRow row in oDS.Tables["Databases"].Ro
{
strRow = row["TableCatalog"].ToStri
oLI = new ListItem(strRow,strRow);
dropDatabase.Items.Add( oLI );
}
this makes a SINGLE call to the row["TableCatalog"].ToStri
AW
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
ASKER
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.
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.