M
mosipenko
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:
<aspataList 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>
</aspataList>
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.
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:
<aspataList 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>
</aspataList>
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.