如何读取空数据 [英] How to read empty data

查看:89
本文介绍了如何读取空数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经遇到过DBNull的问题,但现在我的问题是:


System.InvalidOperationException:没有数据时读取无效

是现在


这里是我的代码背后:


Private Sub Page_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Me.Load

Dim connectionString As String =" Data Source = server; Initial

Catalog = database;用户ID = user;密码=密码;"

Dim连接为SqlClient.SqlConnection

Dim命令As SqlClient.SqlCommand

Dim reader As SqlClient.SqlDataReader

Dim strID As String

strID = Request.QueryString(" cidL")

试试

connection = New SqlClient.SqlConnection(connectionString)

command = New SqlClient.SqlCommand()

command.CommandText =" select top 10 a.cid,a.title ,

dbo.GetImagePathNoDT(a.cid)as ImagePath,Body as DisplayText from

Article an inner join s s sid = a.cid inner join

ContentIndex x on x.cid = a.cid inner join contentcategory cc on cc.cid

= a.cid其中cc.catid = 185和s.startdt之间的GetDate()并且

s.enddt和(x.ownerpid ='''HT8071'')按照updateddt desc命令"

command.Connection = connection

connection.Open()

reader = command.ExecuteReader()

DataList1.DataSource = reader

DataList1.DataBind()

最后

''如果没有连接则没有那么

''connection.Dispose()

''结束如果

''如果Not命令什么都没有那么

''command.Dispose( )

''结束如果

如果读者(ImagePath)IsNot System.DBNull.Value那么

会话(" Image" )=读者(ImagePath)

Else

会话(" Image")=" logo.jpg"

结束If

结束尝试


结束子


在我的页面上,这是显示所有内容的代码来自db:


< asp:DataList ID =" DataList1" RUNAT = QUOT;服务器" DataKeyField =" cid">

< ItemTemplate>

cid:

< asp:Label ID =" cidLabel" RUNAT = QUOT;服务器" Text =''<%#

Eval(" cid")%>''value =" cidL">< / asp:Label>< br />

title:

< asp:Label ID =" titleLabel" RUNAT = QUOT;服务器" Text =''<%#

Eval(" title")%>''>< / asp:Label>< br />

ImagePath:

< asp:Label ID =" ImagePathLabel" RUNAT = QUOT;服务器" Text =''<%#

Eval(" ImagePath")%>''>< / asp:Label>< br />

< asp:TemplateColumn>

< ItemTemplate>

< img src =''\ SuperNova \ content\images \<%#

System.IO.Path.GetFileName(会话(" Image"))%>''width =" 150px">


< / ItemTemplate>

< / asp:TemplateColumn>

DisplayText:

< asp:Label ID =" DisplayTextLabel" runat =" server"

Text =''<%#Eval(" DisplayText")%>''>

< / asp:Label> ;< br />

< br />

< / ItemTemplate>

< / asp:DataList>

有些文章没有图像。文章文字和图像是存储在2个表格中的,因此如果文章没有图像,则图像表中没有

记录。只显示路径是没问题的,

如果没有图像就会空白。问题是当我试图显示实际图像时,我想要显示实际图像。

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

System.InvalidOperationException: 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.EventArgs) Handles Me.Load
Dim connectionString As String = "Data Source=server;Initial
Catalog=database; User ID=user;Password=password;"
Dim connection As SqlClient.SqlConnection
Dim command As SqlClient.SqlCommand
Dim reader As SqlClient.SqlDataReader
Dim strID As String
strID = Request.QueryString("cidL")
Try
connection = New SqlClient.SqlConnection(connectionString)
command = New SqlClient.SqlCommand()
command.CommandText = "select top 10 a.cid, a.title,
dbo.GetImagePathNoDT(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.Connection = connection
connection.Open()
reader = command.ExecuteReader()
DataList1.DataSource = reader
DataList1.DataBind()
Finally
''If Not connection Is Nothing Then
''connection.Dispose()
''End If
''If Not command Is Nothing Then
''command.Dispose()
''End If
If reader("ImagePath") IsNot System.DBNull.Value Then
Session("Image") = reader("ImagePath")
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="cid">
<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="ImagePathLabel" runat="server" Text=''<%#
Eval("ImagePath") %>''></asp:Label><br />
<asp:TemplateColumn>
<ItemTemplate>
<img src=''\SuperNova\content\images\<%#
System.IO.Path.GetFileName(Session("Image"))%>'' width="150px">

</ItemTemplate>
</asp:TemplateColumn>
DisplayText:
<asp:Label ID="DisplayTextLabel" runat="server"
Text=''<%# Eval("DisplayText") %>''>
</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.

推荐答案

在你的最后阻止你试图从datareader读取一些东西。

此时,你的数据主义者已经绑定了它,并且datareader是

超过了最后一行结果集 - 意思是,你不能从

得到任何东西,因为整个结果已被datalist绑定读取。


所以你可以你做你在那里做的事情。


一般来说,当你试图从数据加载器中获取数据时会发生这个错误,

但它是没有指向结果集中的一行 - 要么因为查询没有返回行

,那么datareader还没有定位到结果中的第一行

,或者读者已经超过最后一条记录(这是你的情况,这是你的b $ b)。


< mo ******* @ hotmail.com> ;在消息中写道

news:11 ********************* @ t31g2000cwb.googlegro ups.com ...
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*******@hotmail.com> wrote in message
news:11*********************@t31g2000cwb.googlegro ups.com...
我曾经遇到过DBNull的问题,但现在我的问题是:

System.InvalidOperationException:当没有数据存在时读取无效
这里是我的代码背后:

Private Sub Page_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Me.Load
Dim connectionString As String =" Data Source = server; Initial
Catalog = database;用户ID = user;密码=密码;"
Dim连接为SqlClient.SqlConnection
Dim命令为SqlClient.SqlCommand
Dim reader As SqlClient.SqlDataReader
Dim strID As String
strID = Request.QueryString(" cidL")
尝试
connection = New SqlClient.SqlConnection(connectionString)
command = New SqlClient.SqlCommand()
命令。 CommandText ="选择前10个a.cid,a.title,
dbo.GetImagePathNoDT(a.cid)作为ImagePath,Body作为DisplayText来自
文章内部联接调度s on s.cid = a .cid内连接
ContentIndex x on x.cid = a.cid内连接contentcategory cc on cc.cid
= a.cid其中cc.catid = 185和getDate()介于s.startdt和
s.enddt和(x.ownerpid =''HT8071'')按updatedt desc命令
命令。连接=连接
connection.Open()
reader = command.Exe cuteReader()
DataList1.DataSource = reader
DataList1.DataBind()
最后
''如果Not connection is Nothing那么
''connection.Dispose()< br'>''结束如果
''如果Not命令什么都没有那么
''command.Dispose()
''结束如果
如果读者(ImagePath) IsNot System.DBNull.Value然后
会话(图像)=读者(ImagePath)
其他会议(" Image")=" logo.jpg" <结束如果
结束尝试

结束子

在我的页面上,这是显示db中所有内容的代码:

< asp:DataList ID =" DataList1" RUNAT = QUOT;服务器" DataKeyField =" cid">
< ItemTemplate>
cid:
< asp:Label ID =" cidLabel" RUNAT = QUOT;服务器" Text =''<%#
Eval(" cid")%>''value =" cidL">< / asp:Label>< br />
title :
< asp:Label ID =" titleLabel" RUNAT = QUOT;服务器" Text =''<%#
Eval(" title")%>''>< / asp:Label>< br />
ImagePath:
< ; asp:Label ID =" ImagePathLabel" RUNAT = QUOT;服务器" Text =''<%#
Eval(" ImagePath")%>''>< / asp:Label>< br />
< asp:TemplateColumn>
< ItemTemplate>

< img src =''\ SuperNova \content\images \<%#
System.IO.Path.GetFileName(Session) (""))%>''width =" 150px">

< / ItemTemplate>
< / asp:TemplateColumn>
DisplayText:
< asp:Label ID =" DisplayTextLabel" runat =" server"
Text =''<%#Eval(" DisplayText")%>''>
< / asp:Label>< br />
< br />
< / ItemTemplate>
< / asp:DataList>

有些文章没有图片。文章文字和图像存储在2个表格中,因此如果文章没有图像,则图像表中没有
记录。只显示路径是没问题的,如果没有图像,这将只是空白。问题是当我试图显示实际图像时。
I used to have a problem with DBNull, but now my issue is:

System.InvalidOperationException: 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.EventArgs) Handles Me.Load
Dim connectionString As String = "Data Source=server;Initial
Catalog=database; User ID=user;Password=password;"
Dim connection As SqlClient.SqlConnection
Dim command As SqlClient.SqlCommand
Dim reader As SqlClient.SqlDataReader
Dim strID As String
strID = Request.QueryString("cidL")
Try
connection = New SqlClient.SqlConnection(connectionString)
command = New SqlClient.SqlCommand()
command.CommandText = "select top 10 a.cid, a.title,
dbo.GetImagePathNoDT(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.Connection = connection
connection.Open()
reader = command.ExecuteReader()
DataList1.DataSource = reader
DataList1.DataBind()
Finally
''If Not connection Is Nothing Then
''connection.Dispose()
''End If
''If Not command Is Nothing Then
''command.Dispose()
''End If
If reader("ImagePath") IsNot System.DBNull.Value Then
Session("Image") = reader("ImagePath")
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="cid">
<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="ImagePathLabel" runat="server" Text=''<%#
Eval("ImagePath") %>''></asp:Label><br />
<asp:TemplateColumn>
<ItemTemplate>
<img src=''\SuperNova\content\images\<%#
System.IO.Path.GetFileName(Session("Image"))%>'' width="150px">

</ItemTemplate>
</asp:TemplateColumn>
DisplayText:
<asp:Label ID="DisplayTextLabel" runat="server"
Text=''<%# Eval("DisplayText") %>''>
</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.



那么更好的方法是什么?我还是asp.net的新手

So what''s a better way of doing it? I''m still new to asp.net


< mo ******* @ hotmail.com>在消息中写道

新闻:11 ********************** @ t31g2000cwb.googlegr oups.com ......
<mo*******@hotmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
那么有什么更好的方法呢?我还是asp.net的新手
So what''s a better way of doing it? I''m still new to asp.net




你在Finally子句中实际想要实现的目标是什么......?



What are you actually trying to achieve in your Finally clause...?


这篇关于如何读取空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆