473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

DataReader Error if DB Column blank

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
Nov 18 '05 #1
1 1203
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" <da*********@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
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

Nov 18 '05 #2

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

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Jake S | last post by:
Hi all, Is it possibele to set the datasource of a dropdown list to a datareader? When I try to the only column I receive is a column populated by the datasource name repeated the amount of...
5
by: Catalin Porancea | last post by:
Whenever I try this: ____________________________________________________________________ Dim cmd_sp_payments As SqlCommand = New SqlCommand("sp_payments", con) cmd_sp_payments.CommandType =...
1
by: BC | last post by:
Hi, Been searching the newgroups but haven't found an answer. I'm used to binding disconnected ADO recordsets to Datagrid controls and tweaking the Datagrid to suit my needs (columnheader,...
4
by: Shapper | last post by:
Hello, I have this code to bind a DataGrid to a DataReader when page loads: Sub Page_Load(Sender As Object, e As EventArgs) dgContent.DataSource = drContent dgContent.DataBind() End Sub ...
4
by: Shapper | last post by:
Hello, I have created a datareader function in a asp.net/vb web site. The datareader returns only one record with 2 fields: and In the same aspx.vb I want to use this values as follows:...
3
by: RSH | last post by:
Hi, I have a situation where I have two datareaders, and I want to make sure any given field from Datareader A exists in Datareader B before I can do anything with that column. I tried the code...
3
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, How can I get value of a SQL column defined as int. I try int myInt = dataReader.GetInt32(0); I get invalid cast exception and same for GetInt16...
1
by: =?UTF-8?B?QXJuZSBWYWpow7hq?= | last post by:
JB wrote: if(!reader.IsDBNull(swimcol)) { // use (string)reader } else { // use some default value maybe null
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...

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.