GetString(4) is null

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

I have a SQL database and some fields may be null.

This will trow an exeption:
string[] s=new string[10];
s[0]=String.Format("{0}",myReader.GetString(1));
s[1]=String.Format("{0}",myReader.GetString(2));
s[2]=String.Format("{0}",myReader.GetString(3)); // null field
s[3]=String.Format("{0}",myReader.GetString(4));
One solution is to use:

try { s[0]=String.Format("{0}",myReader.GetString(1)); }
catch {}
try { s[1]=String.Format("{0}",myReader.GetString(2)); }
catch {}
try { s[2]=String.Format("{0}",myReader.GetString(3)); }
catch {}
try { s[3]=String.Format("{0}",myReader.GetString(4)); }
catch {}

Is there a better way to solve null fields ?
 
GTi said:
I have a SQL database and some fields may be null.

This will trow an exeption:
string[] s=new string[10];
s[0]=String.Format("{0}",myReader.GetString(1));
s[1]=String.Format("{0}",myReader.GetString(2));
s[2]=String.Format("{0}",myReader.GetString(3)); // null field
s[3]=String.Format("{0}",myReader.GetString(4));
One solution is to use:

try { s[0]=String.Format("{0}",myReader.GetString(1)); }
catch {}
try { s[1]=String.Format("{0}",myReader.GetString(2)); }
catch {}
try { s[2]=String.Format("{0}",myReader.GetString(3)); }
catch {}
try { s[3]=String.Format("{0}",myReader.GetString(4)); }
catch {}

Is there a better way to solve null fields ?

Yes - use SqlDataReader.IsDBNull to test the columns. Do you definitely
have to read a data reader rather than a data adapter and data set?
 
Jon Skeet said:
GTi said:
I have a SQL database and some fields may be null.

This will trow an exeption:
string[] s=new string[10];
s[0]=String.Format("{0}",myReader.GetString(1));
s[1]=String.Format("{0}",myReader.GetString(2));
s[2]=String.Format("{0}",myReader.GetString(3)); // null field
s[3]=String.Format("{0}",myReader.GetString(4));
One solution is to use:

try { s[0]=String.Format("{0}",myReader.GetString(1)); }
catch {}
try { s[1]=String.Format("{0}",myReader.GetString(2)); }
catch {}
try { s[2]=String.Format("{0}",myReader.GetString(3)); }
catch {}
try { s[3]=String.Format("{0}",myReader.GetString(4)); }
catch {}

Is there a better way to solve null fields ?

Yes - use SqlDataReader.IsDBNull to test the columns. Do you definitely
have to read a data reader rather than a data adapter and data set?

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Yes - use SqlDataReader.IsDBNull to test the columns.
Ok - tanks....
Do you definitely have to read a data reader rather than a data adapter
and data set?
Not really, just that I have not learn to use data adapter and data set
yet...
Using data reader is familiar for me from the old C world.
So if U have a good site that explain using data adapter and data set I will
be grateful.
 
GTi said:
Not really, just that I have not learn to use data adapter and data set
yet...
Using data reader is familiar for me from the old C world.
So if U have a good site that explain using data adapter and data set I will
be grateful.

Just a search for ADO.NET tutorials should find you plenty. It's very
rare that you really need to use a DataReader yourself - they're
generally worth avoiding, preferring to let the DataAdapter do the hard
work :)
 
Back
Top