473,583 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ 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 8011
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.I sDBNull 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.co m>
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.co m> skrev i melding
news:MP******** *************** *@msnews.micros oft.com...
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.I sDBNull 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Yes - use SqlDataReader.I sDBNull 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.co m>
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
6126
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 to retrieve a folder path into the array. When I use the GetString method, the length of the returned string is always 256, even if the actual path...
1
2098
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 file.?????..
6
4730
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" label1.text=cate but there is error for dr.getstring(1), the error reads:the data of this
4
14309
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 is a long string contain string("ABC") and many '0',
0
1723
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 is Class Library In Propeties of resource file Build Action is Embedded Resource and in the strating function i m doin this Public LocRM As...
0
1174
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 is Class Library In Propeties of resource file Build Action is Embedded Resource and in the strating function i m doin this Public LocRM As...
0
1101
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 Type is "Class Library" In Propeties of resource file Build Action is "Embedded Resource" and in the strating function i m doin this Public LocRM As...
3
2825
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 strLastName = rs.getString( "LastName" );
1
1356
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 photo manager. // using the GetString method propItem = img.GetPropertyItem(270); img_desc = Encoding.UTF8.GetString(propItem.Value);
0
8327
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7935
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6579
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5701
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5374
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3818
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2333
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 we have to send another system
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.