Accessing DB using SQL

Posted on 2005-03-28
Medium Priority
Last Modified: 2010-04-07
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

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

HeadlineString = MyReader.GetValue(0)

  End Sub

Question by:interclubs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 12

Accepted Solution

laotzi2000 earned 1800 total points
ID: 13649879
MyReader = MyCommand.ExecuteReader

HeadlineString = MyReader.GetValue(0)

You should call

Author Comment

ID: 13649955
One last quick question on it, how do I think access the DB value in the page body. <%=headlineString%> is giving me errors.....
LVL 12

Expert Comment

ID: 13649966
Dim headlineString as String
out of Page_Load
and make it
Public headlineString as String

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question