Re: GetString(4) is null

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"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.




.



Relevant Pages

  • Re: GetString(4) is null
    ... just that I have not learn to use data adapter and data set ... > 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 ... If replying to the group, please do not mail me too ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Data Sets Vs Data Readers
    ... using the Data Adapter and DataSet combination you are leveraging the fact ... handle your connections for you). ... It may be tempting to consider that you can simply use a data reader bound ... when Looping through a Data reader, what kind of Locks are ...
    (microsoft.public.dotnet.framework.adonet)
  • Cannot access Access database
    ... I am a new user developing a Web form in vsnet. ... connection, a data adapter and a data set. ... When I ask to preview the data from the data adapter properties box I ...
    (microsoft.public.vsnet.general)
  • Re: Convert Data Reader into DataSet
    ... I wite with n-tier components and the reason I ... pass the data reader from a component back to ... I know in classic asp this would take a performance hit. ... can datareaders be passed back from a data set with out ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Reserved Keywords
    ... > right clicking and generating a data set from that data adapter? ... > you run the SQL statement the columns returned are: ... > parser to select a column with the exact name, ...
    (microsoft.public.dotnet.framework.adonet)