Saturday, March 31, 2012

Senility Setting In?

I've been working on some differnt projects for a while and I just got back to an ASP.NET 2.0 (VB) project and I seem to have forgotten (screwed up?) some basics. I created a new form (using a master page) and I simply want to read and use an integer value from a SQL Table (see code below). In this code, "conn" gets flagged as "Declaration expected" in the Open and Close statements (but it is OK in the Dim cmd1... statement?). I want to retrieve the residentid integer value from the table and pass it as a query parameter in the link to residentfeature.aspx - I can't find the correct syntax to accomplish that either.

I know I must be missing something very basic here but I can't see it?

================ Code ================

<%

@dotnet.itags.org.ImportNamespace="System.Data" %>

<%

@dotnet.itags.org.ImportNamespace="System.Data.SQLClient" %>

<%@dotnet.itags.org. Page Language="VB" MasterPageFile="~/Default.master" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Dim residentid As Integer
<script runat="server">
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("GoodSamSiteDB").ConnectionString)
Dim cmd1 As New SqlCommand("SELECT resident From Features", conn)
conn.Open()
residentid = cmd1.ExecuteScalar()
conn.Close()
</script>
<div id="body">
<div class="fullwidth">
<h2>Weekly Features</h2>
<h3>Featured Resident</h3>
<a href="http://links.10026.com/?link=residentfeature.aspx?id=" + residentid + >Meet Mary Jones</a>
</div>
</div>
</asp:Content>

I am no VB expert (hence the vcs in my name) but I think this will remedy the problem:
Dim connAs SqlConnection =New SqlConnection(ConfigurationManager.ConnectionStrings("GoodSamSiteDB").ConnectionString)Dim cmd1As SqlCommand =New SqlCommand("SELECT resident From Features", conn)

Also, ExecuteScalar returns object, so to assign it as an Integer, you would need to cast it or use Int32.Parse() or Int32.TryParse()

Kevin;

Thanks for the response but that change had no effect?


try this
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script runat="server">
Dim residentid As Integer
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("GoodSamSiteDB").ConnectionString)
Dim cmd1 As New SqlCommand("SELECT resident From Features", conn)
conn.Open()
residentid = cmd1.ExecuteScalar()
conn.Close()
End Sub
</script>
<a href="http://links.10026.com/?link=residentfeature.aspx?id=<%= residentid %>">Meet Mary Jones</a>
or
<asp:HyperLink id=HyperLink1 Text="Meet Mary Jones" NavigateUrl='<%# "detailspage_vb.aspx?id=" + residentid%>' runat="server"/>
you needed to wrap the code in an event

0 comments:

Post a Comment