473,473 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GetString(4) is null

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 ?


Nov 17 '05 #1
3 7996
GTi <bl****@blabla.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
GTi

"Jon Skeet [C# MVP]" <sk***@pobox.com> skrev i melding
news:MP************************@msnews.microsoft.c om...
GTi <bl****@blabla.com> 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 - <sk***@pobox.com>
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.


Nov 17 '05 #3
GTi <bl****@blabla.com> wrote:
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.


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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul K | last post by:
I've run into a problem using this method that I have been able to work around, but I think that there is a more elegant way. I create an array of 256 bytes and use the SHBrowseForFolders API...
1
by: perspolis | last post by:
Hi My application worked well,but something wrong with it.. I don't know. I can't add any form components to it anymore. resource.GetString(".Text") return null but ths .Text exist in ..resx...
6
by: Nakhi | last post by:
Hi, my code is very simple. dim cm as oledbcommand= new oledbcommand("select * from page " ,cn) cn.open() Dim dr As OleDbDataReader = cm.ExecuteReader() dim cate=dr.getstring(1) 'error line"...
4
by: cok | last post by:
Hi, All I have a question about Encoding.ASCII.GetString. I want convert a byte array to a string, I use Encoding.ASCII.GetString(byteArray) (byteArray is something like ) but the retult...
0
by: sagar | last post by:
Hi, I m having a vb.net project in which i created a default resource file Resource.resx as rightclick on project Add Add new Item resource assembly file In properties of project OutPut Type...
0
by: vabby | last post by:
Hi, I m having a vb.net project in which i created a default resource file Resource.resx as rightclick on project Add Add new Item resource assembly file In properties of project OutPut Type...
0
by: sagar | last post by:
Hi, I m having a vb.net project in which i created a default resource file Resource.resx as rightclicking on project Add Add new Item resource assembly file In properties of project Output...
3
by: Frank Milverckowitz | last post by:
Hi, Newbie question about SqlDataReader column value access... In java jdbc code to get a value from a table column we can pass the column name (instead of an int index or offset): String...
1
by: mikfrost | last post by:
I'm trying to read the exif info in a JPG image file. Reading the ImageDescription field (270) that contains "abcæåø" only reads the first 3 characters "abc". The field was written with ACDSee...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.