Re: GetString(4) is null
- From: "GTi" <blabla@xxxxxxxxxx>
- Date: Mon, 30 May 2005 09:46:18 +0200
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> skrev i melding
news:MPG.1d04d1e76c0f520c98c222@xxxxxxxxxxxxxxxxxxxxxxx
> GTi <blabla@xxxxxxxxxx> wrote:
>> 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 - <skeet@xxxxxxxxx>
> 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.
.
- Follow-Ups:
- Re: GetString(4) is null
- From: Jon Skeet [C# MVP]
- Re: GetString(4) is null
- References:
- GetString(4) is null
- From: GTi
- Re: GetString(4) is null
- From: Jon Skeet [C# MVP]
- GetString(4) is null
- Prev by Date: Display DataTable content reversely
- Next by Date: Image size?
- Previous by thread: Re: GetString(4) is null
- Next by thread: Re: GetString(4) is null
- Index(es):
Relevant Pages
|