I'm using dataReader but if there's a Null it creates a Error

G

Guest

in a page_load event on a page I am doing, I'm filling textboxes with data from a SQL Server DB

I'm reading using Datareader but if a column contains a NULL, the page Errors.

I realise the datareader firstly retrieves a true/false but how do i use this without impacting my code loads?? I'm stuck and need assistance urgently if poss please

****start of my cod

cn = New SqlConnection("xxxx"
cn.Open(
cmd = New SqlCommand("SELECT * FROM Table Details WHERE Email = @EMail", cn
cmd.parameters.add("@Email", Email.text
rdr = cmd.ExecuteReader(
rdr.Read(

'fill details text boxes on the scree
txtTitle.Text = rdr("Title"
Fname.Text = rdr("FirstName"

****

So, if there is a <NULL> in the FirstName it errors. Please hel
 
P

Patrice

You'll have to use the IsDBNUll function to check for null values...

Patrice

dazzalondon said:
in a page_load event on a page I am doing, I'm filling textboxes with data from a SQL Server DB.

I'm reading using Datareader but if a column contains a NULL, the page Errors.

I realise the datareader firstly retrieves a true/false but how do i use
this without impacting my code loads?? I'm stuck and need assistance
urgently if poss please.
 
G

Guest

hi Patric
Thank

but any chance you can give me an example

The text boxes i'm filling onscreen runs into about 15 text boxes so don't want to impact my code too much!
 
W

William Ryan eMVP

A few suggestions first
First off, turn on Option Strict ;-)
Next, use either the GetOrdinalMethod or use Bill Vaughn's Enum method

Public Enum ColumnValues
Title
, FirstName
End Enum

and replace those column names with readable, yet index based lookups, will
speed things up for you quite a bit.

Since neither of them is the problem though.

You can use an IIF and IsDbNull to check for Null values and then replace
them with something that won't throw an exception. Or you can create a
function that does this for you and just pass each value to the function.
Or you can use the IsNull function in the query so that you guarantee that
you don't get any nulls back. All three will yeild ultimately the same
results.

Also, unless Table_Details only has two columns, then you may want to switch
your query around b/c you are only using two values. Similarly, if you are
using the 1.1 framework, you can use the rdr.HasRows to determine if
anything was returned. If the user entered in junk b/c they misttyped for
instance, you may not get anything back. so no need calling Read or setting
the textbox values, you know nothing is there.

I know I added a lot of extraneous stuff, but figured you may be interested.

Cheers,

Bill
dazzalondon said:
in a page_load event on a page I am doing, I'm filling textboxes with data from a SQL Server DB.

I'm reading using Datareader but if a column contains a NULL, the page Errors.

I realise the datareader firstly retrieves a true/false but how do i use
this without impacting my code loads?? I'm stuck and need assistance
urgently if poss please.
 
P

Patrice

Done by William !

As a side note if you don't care about taking into account the fact that the
Title or the LastName is not known (instead of just left "blank"), you could
disallow NULL and use an empty string instead...

Patrice

dazzalondon said:
hi Patrice
Thanks

but any chance you can give me an example?

The text boxes i'm filling onscreen runs into about 15 text boxes so don't
want to impact my code too much!
 
G

Guest

I agree with Patrice and William
However, for null string data, there is a simpler way
Fname.Text = rdr("FirstName") & "

Bin Song, MCP
 
G

Guest

cheers guy

I needed a easy solution so took out the null's in my DB.; though I would have opted for the 'Fname.Text = rdr("FirstName") & ""' solutio

You guys are usually rpetty quick, so please please pleas have a look at my posting above - titled
"UPDATE using params, again, deosn't work

I've been on this all day and am supposed to finsih this website tonight. that isn't going to happen because of these stupid errors being made. BEcause of my set-up i have to publish the 1.5mb bin file each time i need to view. That is sooooo annoyiong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top