Accessing DB using SQL

I can't get the below code to work. It keeps giving me the error "Invalid attempt to read when no data is present."... Also, instead of MyReader.GetValue(0) how I do it so that I refer to the field names, such as MyReader.GetValue("Headline")?

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>

<script language="VB" runat="server">

  Public Sub Page_Load(sender as object,e as eventargs)
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Dim strSQL as String = "select Headline, PubDate, Teaser, Author, CLB, CURLs, Active, BodyText from CSL_TBLNews where NewsID=" & request("id") & " order by PubDate asc"


Dim MyCommand As SqlCommand
Dim MyReader As SqlDataReader

Dim headlineString as String

myConnection.Open()
MyCommand = New SqlCommand(strSQL, MyConnection)
MyReader = MyCommand.ExecuteReader

HeadlineString = MyReader.GetValue(0)

Response.Write(HeadlineString)
  End Sub
</script>

interclubsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
laotzi2000Connect With a Mentor Commented:
between
MyReader = MyCommand.ExecuteReader

HeadlineString = MyReader.GetValue(0)

You should call
MyReader.Read
0
 
interclubsAuthor Commented:
One last quick question on it, how do I think access the DB value in the page body. <%=headlineString%> is giving me errors.....
0
 
laotzi2000Commented:
Move
Dim headlineString as String
out of Page_Load
and make it
Public headlineString as String
0
All Courses

From novice to tech pro — start learning today.