DataReader Error if DB Column blank

G

Guest

Hi there.

I have various pages in an ASP.NET applicaiton and in the Page_load event I have data being read from a SQL DB filling textboxes on the page

Now, sometimes the fields in the DB will be blank and I would a 'Invalid attempt to read when no data is present.' error.

I have tried to implement validation in pages to ease this issue but I need a solution which covers just the below - reading a DB where it doesn't fail (I have received suggestions before, but never examples of code).

Code I have:

Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim rdr As SqlDataReader
cn = New SqlConnection("xxx")
cmd = New SqlCommand("SELECT * FROM MemberTable WHERE Email=@Email", cn)
cmd.Parameters.Add("@Email", EmailVar)

cn.Open()
rdr = cmd.ExecuteReader()
rdr.Read()

txt1.Text = "" & rdr("FirstName")
txt2.Text = "" & rdr("LastName")
txt3.Text = "" & rdr("dateOfBirth")

rdr.Close()

Many thanks
 
C

Cristian Suazo

Hi,

try putting the read part in an if statement (it returns true if it's able
to read). Something like this:

If rdr.Read() = true Then
txt1.Text = "" & rdr("FirstName")
txt2.Text = "" & rdr("LastName")
txt3.Text = "" & rdr("dateOfBirth")
End If


Hope this helps
/Cristian



dazzalondon said:
Hi there.

I have various pages in an ASP.NET applicaiton and in the Page_load event
I have data being read from a SQL DB filling textboxes on the page
Now, sometimes the fields in the DB will be blank and I would a 'Invalid
attempt to read when no data is present.' error.
I have tried to implement validation in pages to ease this issue but I
need a solution which covers just the below - reading a DB where it doesn't
fail (I have received suggestions before, but never examples of code).
 

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