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("Headlin e")?
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.Sql Client" %>
<script language="VB" runat="server">
Public Sub Page_Load(sender as object,e as eventargs)
Dim myConnection as New SqlConnection(Configuratio nSettings. AppSetting s("Connect ionString" ))
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(HeadlineStr ing)
End Sub
</script>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.Sql
<script language="VB" runat="server">
Public Sub Page_Load(sender as object,e as eventargs)
Dim myConnection as New SqlConnection(Configuratio
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(HeadlineStr
End Sub
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Move
Dim headlineString as String
out of Page_Load
and make it
Public headlineString as String
Dim headlineString as String
out of Page_Load
and make it
Public headlineString as String
ASKER