PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

dataReader returns an error if field blank

 
 
=?Utf-8?B?ZGF6emFsb25kb24=?=
Guest
Posts: n/a
 
      15th Jun 2004
Hey there

I know there's a simple answer to this Quesiton because I've asked it before (but I have not yet used it and have now forgotten what the answer was!) so..

I have a simple Datareader returning the value of a field from a SQL Server but if the field is blank, the application returns an error.

A piece of the code is as follows:

cmd = New SqlCommand("SELECT * FROM thisTable WHERE Email = '" & memberVariable & "'", cn)
rdr = cmd.ExecuteReader()
rdr.Read()
textField1.Text = rdr("column1")
textField2.Text = rdr("column2")


I remember the solution was something like
textField2.Text = rdr("column2") & ""

but this returns the same error.

Help!


 
Reply With Quote
 
 
 
 
William Ryan eMVP
Guest
Posts: n/a
 
      15th Jun 2004
You can check for IsDbNull and that should do it for you.

However, a few misc comments. 1) You are using Dynamic Sql - change it to
paramaterized queries http://www.knowdotnet.com/articles/dynamisql.html .
The newest version of Les' refactoring tool
http://www.knowdotnet.com/articles/n...oducthome.html will even do
all the heavy lifting for you. If someone entered ' or 1=1 ; Drop Table
thisTable for the value of memberValue, you'd be in for a bad day ;-)
2) Instead of using an excecutereader statement, you may want to consider
using either ExcecuteScalar or an OutPut parameter. Not a big deal but
probably more efficient. If you check out www.betav.com ->Articles -> MSDN,
Bill Vaughn has probably the best discussion of it I've come across in
Retrieving the Gozoutas (or bringing home the Gozoutas). By using an output
param you can just check the value of the parameter. Another thing you can
do is use COUNT(*) and check the value since it can't ever be db null, it'll
be 0 if nothing's found

Also, it won't matter much in this instance but in general, use a numeric
index instead of the column name. Better yet though is calling GetOrdinal
of the column so you can use a Variable name that gives you the clarity of
referencing the columnname but the performance of using an index based
lookup. Or you can use Vaughn's method of using an enum, givng you the best
possible performance and readability.

HTH,

Bill

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
"dazzalondon" <(E-Mail Removed)> wrote in message
news:4B123080-CE1F-4DD8-A62C-(E-Mail Removed)...
> Hey there
>
> I know there's a simple answer to this Quesiton because I've asked it

before (but I have not yet used it and have now forgotten what the answer
was!) so..
>
> I have a simple Datareader returning the value of a field from a SQL

Server but if the field is blank, the application returns an error.
>
> A piece of the code is as follows:
>
> cmd = New SqlCommand("SELECT * FROM thisTable WHERE Email = '" &

memberVariable & "'", cn)
> rdr = cmd.ExecuteReader()
> rdr.Read()
> textField1.Text = rdr("column1")
> textField2.Text = rdr("column2")
>
>
> I remember the solution was something like
> textField2.Text = rdr("column2") & ""
>
> but this returns the same error.
>
> Help!
>
>



 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      15th Jun 2004
The first thing you need, is to turn option strict on. This forces you to
think about things like that and not rely on late binding (which makes code
less efficient and less readable).

Once you do that, you will see that you have to turn this into a string,
because what gets returned is an object. If this is a char or varchar type
column in the database, that could be a String being returned or a DBNull.
That means you have to handle both cases.

This could mean having an if statement that checks for DBNull. It could
also mean calling .ToString() on what is being returned, as the ToString
method of DBNull return the empty string, which is probably what you want in
this scenario.

"dazzalondon" <(E-Mail Removed)> wrote in message
news:4B123080-CE1F-4DD8-A62C-(E-Mail Removed)...
> Hey there
>
> I know there's a simple answer to this Quesiton because I've asked it

before (but I have not yet used it and have now forgotten what the answer
was!) so..
>
> I have a simple Datareader returning the value of a field from a SQL

Server but if the field is blank, the application returns an error.
>
> A piece of the code is as follows:
>
> cmd = New SqlCommand("SELECT * FROM thisTable WHERE Email = '" &

memberVariable & "'", cn)
> rdr = cmd.ExecuteReader()
> rdr.Read()
> textField1.Text = rdr("column1")
> textField2.Text = rdr("column2")
>
>
> I remember the solution was something like
> textField2.Text = rdr("column2") & ""
>
> but this returns the same error.
>
> Help!
>
>



 
Reply With Quote
 
=?Utf-8?B?ZGF6emFsb25kb24=?=
Guest
Posts: n/a
 
      15th Jun 2004
thanks both

But I really need a quick solution.

..... or could you example your suggested routes, as I am unclea (aka a bit thick!).

Many thanks
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static Method returns DataReader Varangian Microsoft ASP .NET 4 8th Jan 2007 03:19 PM
DataReader .HasRows but returns 'Invalid attempt to read when no data is present' error Assimalyst Microsoft ADO .NET 3 8th May 2006 02:16 PM
IIf statement blank field returns all data =?Utf-8?B?U0pI?= Microsoft Access 10 13th Oct 2004 02:44 AM
DataReader Error if DB Column blank =?Utf-8?B?ZGF6emFsb25kb24=?= Microsoft ASP .NET 1 20th Jun 2004 04:50 PM
Help: Function that returns DataReader VB Programmer Microsoft ADO .NET 1 12th Aug 2003 08:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 AM.