473,662 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read empty data

I used to have a problem with DBNull, but now my issue is:

System.InvalidO perationExcepti on: Invalid attempt to read when no data
is present

Here's my code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles Me.Load
Dim connectionStrin g As String = "Data Source=server;I nitial
Catalog=databas e; User ID=user;Passwor d=password;"
Dim connection As SqlClient.SqlCo nnection
Dim command As SqlClient.SqlCo mmand
Dim reader As SqlClient.SqlDa taReader
Dim strID As String
strID = Request.QuerySt ring("cidL")
Try
connection = New SqlClient.SqlCo nnection(connec tionString)
command = New SqlClient.SqlCo mmand()
command.Command Text = "select top 10 a.cid, a.title,
dbo.GetImagePat hNoDT(a.cid) as ImagePath, Body as DisplayText from
Article a inner join Schedule s on s.cid = a.cid inner join
ContentIndex x on x.cid = a.cid inner join contentcategory cc on cc.cid
= a.cid where cc.catid = 185 and GetDate() between s.startdt and
s.enddt and (x.ownerpid = 'HT8071') order by updateddt desc"
command.Connect ion = connection
connection.Open ()
reader = command.Execute Reader()
DataList1.DataS ource = reader
DataList1.DataB ind()
Finally
'If Not connection Is Nothing Then
'connection.Dis pose()
'End If
'If Not command Is Nothing Then
'command.Dispos e()
'End If
If reader("ImagePa th") IsNot System.DBNull.V alue Then
Session("Image" ) = reader("ImagePa th")
Else
Session("Image" ) = "logo.jpg"
End If
End Try

End Sub

And on my page, this is the code to display all the stuff from the db:

<asp:DataList ID="DataList1" runat="server" DataKeyField="c id">
<ItemTemplate >
cid:
<asp:Label ID="cidLabel" runat="server" Text='<%#
Eval("cid") %>' value="cidL"></asp:Label><br />
title:
<asp:Label ID="titleLabel " runat="server" Text='<%#
Eval("title") %>'></asp:Label><br />
ImagePath:
<asp:Label ID="ImagePathLa bel" runat="server" Text='<%#
Eval("ImagePath ") %>'></asp:Label><br />
<asp:TemplateCo lumn>
<ItemTemplate >
<img src='\SuperNova \content\images \<%#
System.IO.Path. GetFileName(Ses sion("Image"))% >' width="150px">

</ItemTemplate>
</asp:TemplateCol umn>
DisplayText:
<asp:Label ID="DisplayText Label" runat="server"
Text='<%# Eval("DisplayTe xt") %>'>
</asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
Some of the articles don't have images. The article text and image are
stored in 2 tables, so if the article doesn't have an image, there's no
record in the image table. It's no problem just displaying the path,
which will just be blank if there's no image. The problem is when I'm
trying to display the actual image.

Apr 21 '06 #1
15 2015
In your Finally block you are trying to read something from the datareader.
At this point, your datalist has been bound to it, and the datareader is
past the last row in the resultset - meaning, you can't get anything out of
it, since the entire result has been read by the datalist binding.

So you can't do what you are doing there.

In general, this error happens when you try to get data out of a datareader,
but it is not pointing to a row in the result set - either because no rows
were returned from the query, the datareader has not been positioned to the
first row in the result, or the reader is past the last record (which is
your case here).

<mo*******@hotm ail.com> wrote in message
news:11******** *************@t 31g2000cwb.goog legroups.com...
I used to have a problem with DBNull, but now my issue is:

System.InvalidO perationExcepti on: Invalid attempt to read when no data
is present

Here's my code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles Me.Load
Dim connectionStrin g As String = "Data Source=server;I nitial
Catalog=databas e; User ID=user;Passwor d=password;"
Dim connection As SqlClient.SqlCo nnection
Dim command As SqlClient.SqlCo mmand
Dim reader As SqlClient.SqlDa taReader
Dim strID As String
strID = Request.QuerySt ring("cidL")
Try
connection = New SqlClient.SqlCo nnection(connec tionString)
command = New SqlClient.SqlCo mmand()
command.Command Text = "select top 10 a.cid, a.title,
dbo.GetImagePat hNoDT(a.cid) as ImagePath, Body as DisplayText from
Article a inner join Schedule s on s.cid = a.cid inner join
ContentIndex x on x.cid = a.cid inner join contentcategory cc on cc.cid
= a.cid where cc.catid = 185 and GetDate() between s.startdt and
s.enddt and (x.ownerpid = 'HT8071') order by updateddt desc"
command.Connect ion = connection
connection.Open ()
reader = command.Execute Reader()
DataList1.DataS ource = reader
DataList1.DataB ind()
Finally
'If Not connection Is Nothing Then
'connection.Dis pose()
'End If
'If Not command Is Nothing Then
'command.Dispos e()
'End If
If reader("ImagePa th") IsNot System.DBNull.V alue Then
Session("Image" ) = reader("ImagePa th")
Else
Session("Image" ) = "logo.jpg"
End If
End Try

End Sub

And on my page, this is the code to display all the stuff from the db:

<asp:DataList ID="DataList1" runat="server" DataKeyField="c id">
<ItemTemplate >
cid:
<asp:Label ID="cidLabel" runat="server" Text='<%#
Eval("cid") %>' value="cidL"></asp:Label><br />
title:
<asp:Label ID="titleLabel " runat="server" Text='<%#
Eval("title") %>'></asp:Label><br />
ImagePath:
<asp:Label ID="ImagePathLa bel" runat="server" Text='<%#
Eval("ImagePath ") %>'></asp:Label><br />
<asp:TemplateCo lumn>
<ItemTemplate >
<img src='\SuperNova \content\images \<%#
System.IO.Path. GetFileName(Ses sion("Image"))% >' width="150px">

</ItemTemplate>
</asp:TemplateCol umn>
DisplayText:
<asp:Label ID="DisplayText Label" runat="server"
Text='<%# Eval("DisplayTe xt") %>'>
</asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
Some of the articles don't have images. The article text and image are
stored in 2 tables, so if the article doesn't have an image, there's no
record in the image table. It's no problem just displaying the path,
which will just be blank if there's no image. The problem is when I'm
trying to display the actual image.

Apr 21 '06 #2
So what's a better way of doing it? I'm still new to asp.net

Apr 21 '06 #3
<mo*******@hotm ail.com> wrote in message
news:11******** **************@ t31g2000cwb.goo glegroups.com.. .
So what's a better way of doing it? I'm still new to asp.net


What are you actually trying to achieve in your Finally clause...?
Apr 21 '06 #4
This is really more of an ADO.NET problem, not realy related to web
programming.

I have no idea what it is you are trying to do, so it's hard to say.

I am not sure why your are trying to put an Image from session into your
datalist. First off, the binding occurrs *before* you even try to set your
session variable - so first time around, the binding would all happen before
the session variable is set, so no image.

Second of all, if your query is returning the image paths - why not use what
you get from your data? Presumably you are trying to get an image path from
the last row - then make that the image for every row in the list. Why?

<mo*******@hotm ail.com> wrote in message
news:11******** **************@ t31g2000cwb.goo glegroups.com.. .
So what's a better way of doing it? I'm still new to asp.net

Apr 21 '06 #5
That is the way that someone had suggested (on a different post) that I
get my data. What I am trying to do is pull all the articles from the
database that are for a specific category. Then, on the webpage,
display the text and images (if any) for those articles. The articles
are in one table, and if they have an image, it's in a different table.
The article text and imagepath displays fine. The problem I have is
with displaying the actual image (the <img
src='\SuperNova \content\images \<%#
System.IO.Path. GetFileName(Ses sion("Image"))% >' width="150px"> part on
my aspx page). If there is an image it displays, if there is no image I
get an error.

Apr 21 '06 #6
<mo*******@hotm ail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .

<snip>
are in one table, and if they have an image, it's in a different table.
<snip>
src='\SuperNova \content\images \<%#
System.IO.Path. GetFileName(Ses sion("Image"))% >' width="150px">


But this is querying a Session variable, not a record in your DataReader...

???
Apr 21 '06 #7
I originally had it as Eval("ImagePath ") but I changed it for the If
statement to the Session variable based on what the person who
suggested that code told me to do.

My original code for the images was this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles Me.Load
Dim connStr As String = "Data Source=server;I nitial
Catalog=databas e; User ID=user;Passwor d=password;"

Dim connObj As SqlClient.SqlCo nnection = New
SqlClient.SqlCo nnection

Dim connCmd As New SqlClient.SqlCo mmand

Dim strID As String
strID = Request.QuerySt ring("cidL")

Dim selectstr As String = "Select ImagePath from Image where
cid='" + strID + "'"
Dim dataread As SqlClient.SqlDa taReader

Dim unameexists As Boolean

connObj.Connect ionString = connStr

connObj.Open()

connCmd.Command Text = selectstr

connCmd.Connect ion = connObj

dataread = connCmd.Execute Reader()

DataList1.DataB ind()

dataread.Close( )

End Sub

Apr 21 '06 #8
1 problem here, is that you are never closing your connection. You need to
do that.

What was the problem with this code, and how was using a session variable
supposd to solve that problem?

What did the other person actually tell you to do? And how did he/she say
that suggestion would make it work?

<mo*******@hotm ail.com> wrote in message
news:11******** *************@j 33g2000cwa.goog legroups.com...
I originally had it as Eval("ImagePath ") but I changed it for the If
statement to the Session variable based on what the person who
suggested that code told me to do.

My original code for the images was this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles Me.Load
Dim connStr As String = "Data Source=server;I nitial
Catalog=databas e; User ID=user;Passwor d=password;"

Dim connObj As SqlClient.SqlCo nnection = New
SqlClient.SqlCo nnection

Dim connCmd As New SqlClient.SqlCo mmand

Dim strID As String
strID = Request.QuerySt ring("cidL")

Dim selectstr As String = "Select ImagePath from Image where
cid='" + strID + "'"
Dim dataread As SqlClient.SqlDa taReader

Dim unameexists As Boolean

connObj.Connect ionString = connStr

connObj.Open()

connCmd.Command Text = selectstr

connCmd.Connect ion = connObj

dataread = connCmd.Execute Reader()

DataList1.DataB ind()

dataread.Close( )

End Sub

Apr 21 '06 #9
The problem with my original code (the one without the Try...Finally)
was that I get a DBNull to String conversion error. The other person
said I needed to use a Try...Finally to stop it, and gave me that other
code that I posted at the top. All they had in the If statement was
that if the reader was nothing then dispose of it. But what I needed to
do was if the field ImagePath was DBNull, then to replace it with a
different image (the logo.jpg). The other person suggested using the
Session variable in the If statement to do that.

Apr 21 '06 #10

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

Similar topics

15
5739
by: Frank Bormann | last post by:
Hi, probably a stupid question, but I haven't been able to find anything. Is there a istream related function that let me read exactly one keystroke from the keyboard through cin? What I need it to do is this: - remove all characters currently in the input buffer - block until the user presses a key
75
5332
by: Greg McIntyre | last post by:
I have a Python snippet: f = open("blah.txt", "r") while True: c = f.read(1) if c == '': break # EOF # ... work on c Is some way to make this code more compact and simple? It's a bit spaghetti.
18
4877
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
2
26670
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as empty elements in the XML. How to do that ? I don't understand why there is no simple option to specify the output format, or did I miss something ? regards andreas
5
1472
by: Lafayette | last post by:
i´m reading some data from a socket object and when i call the read method it returns 0 bytes to my byte array. my code: private void processRequest(){ byte buffer; int numBytes = this._userSocket.Available; if(numBytes > maxHeaderBytes){
2
1154
by: Stewart Arnold | last post by:
I'm trying to convert a Real Basic routine into Python and I can't read the long integer data from a file. Here is the Real Basic code: b=f.OpenAsBinaryFile b.LittleEndian=True i=b.ReadByte titles=b.ReadLong shows=b.ReadLong
5
9090
by: th3dude | last post by:
I've searched quite a bit for this one and i can't find any solid solution for what i'm doing here. Right now i'm geting an xml string from an API, creating an xml file, then read the file with XPath or XmlReader, grab the attribute data, dump it into the database. Once all that's done i blow away all the xml files and start the whole process over next time i need to read fresh data from the API.
2
3376
by: peter.vanna | last post by:
I would like to write a program to read a text file (data.txt) on Windows and Linux machines. data.txt is formated as follows: 1 10 100 1000 2 20 (a new but empty line)
6
3732
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: class GeneInfo(): def __init__(self): #urllib.urlretrieve('ftp://ftp.ncbi.nih.gov/gene/DATA/ gene_info.gz',"/tmp/gene_info.gz")
3
4376
by: cmdolcet69 | last post by:
Im trying to raed data over a com port. The data comes over the serial port as such reading CRLF reading CRLF and so on. The code below only sees the first reading CRLF and then only added that into the DSIDXPort arraylist and never adds readings 2 through 16..... How can I get it to read those?
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6186
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.