Link to home
Start Free TrialLog in
Avatar of interclubs
interclubs

asked on

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>

ASKER CERTIFIED SOLUTION
Avatar of laotzi2000
laotzi2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of interclubs
interclubs

ASKER

One last quick question on it, how do I think access the DB value in the page body. <%=headlineString%> is giving me errors.....
Move
Dim headlineString as String
out of Page_Load
and make it
Public headlineString as String