Wednesday, March 21, 2012

Paramatized Query Problems!

Hi there,

I am having all sorts of trouble trying to get Paramatized Queries working on my page. The following compilation error is created:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30188: Declaration expected.

Source Error:

Line 23: Dim Rent As String

Line 24: 'Creates parameters for the WHERE statement taking the querystring contents from previous page

Line 25: cmd.Parameters.Add(New SQLParameter("@dotnet.itags.org.CityTown", CityTown))

Line 26: cmd.Parameters.Add(New SQLParameter("@dotnet.itags.org.Suburb", Suburb))

Line 27: cmd.Parameters.Add(New SQLParameter("@dotnet.itags.org.Rooms", Rooms))

Source File: K:\detailspropertylisting.aspx Line: 25

As I am passing the query string from page 1 to page 2 and using the paramatized queries on page 2, I can not just use someID.Text etc to declare my parameters. The code for the page is provided below. Any suggestions you can provide would be greatly appreciated. I have spent hours trying to resolve it.

TCM


<%@dotnet.itags.org. Page Language="VB" %
<%@dotnet.itags.org. Register TagPrefix="xprs" TagName="header" src="http://pics.10026.com/?src=\controls\header.ascx" %
<%@dotnet.itags.org. Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI" Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral, PublicKeyToken=6f763c9966660626" %
<%@dotnet.itags.org. import Namespace="System.Data" %
<%@dotnet.itags.org. import Namespace="System.Data.SqlClient" %
<script runat="server"
Sub Page_Load(sender as Object, e as EventArgs)

' Binds the Data List to the SQLDataSourceControl

dlPropertyListing.DataBind()

'Retreives the Query String content from the previous page

CityTown=request.querystring("CityTown")

Suburb=request.querystring("Suburb")

Rooms=request.querystring("Rooms")

Rent=request.querystring("Rent")

End Sub

'Declares commands

Dim cmd As SqlCommand

Dim CityTown As string

Dim Suburb As string

Dim Rooms As string

Dim Rent As String

'Creates parameters for the WHERE statement taking the querystring contents from previous page

cmd.Parameters.Add(New SQLParameter("@dotnet.itags.org.CityTown", CityTown))

cmd.Parameters.Add(New SQLParameter("@dotnet.itags.org.Suburb", Suburb))

cmd.Parameters.Add(New SQLParameter("@dotnet.itags.org.Rooms", Rooms))

cmd.Parameters.Add(New SQLParameter("@dotnet.itags.org.Rent", Rent))

</script
<html
<head
<link media="screen" href="http://links.10026.com/?link=..\css\default.css" type="text/css" rel="stylesheet" /
</head
<body class="body"
<form runat="server"
<table height="10" width="100%"
<tbody
<tr
<td width="100%"
<xprs:header id="header" runat="server"></xprs:header
</td
</tr
</tbody
</table
<p
</p
<table height="10" width="100%"
<tbody
<tr
<td width="30%"
</td
<td
<p
<font size="5">Properties matching your criteria... </font
</p
<p align="left"
<asp:DataList id="dlPropertyListing" runat="server" DataSource="<%# dsPropertyLisiting %>" DataMember="Table" Width="70%"
<ItemTemplate
<table class="" cellspacing="0" cellpadding="0" width="100%" align="left" border="0"
<tbody
<tr
<td width="12"
<img height="10" src="http://pics.10026.com/?src=../images/box-orange/top-left.gif" width="10" /></td
<td width="100%" background="../images/box-orange/top.gif"
<font size="4"><img height="10" src="http://pics.10026.com/?src=../images/box-orange/sizer.gif" width="1" /></font></td
<td width="12"
<img height="10" src="http://pics.10026.com/?src=../images/box-orange/top-right.gif" width="10" /></td
</tr
<tr
<td background="../images/box-orange/left.gif"
<img height="1" src="http://pics.10026.com/?src=../images/box-orange/sizer.gif" width="10" /></td
<td class=""
<font face="Verdana, Arial, sans-serif" color="gray" size="4"
<p align="center"></p
<p align="center"
<table height="10" width="100%"
<tbody
<tr
<td width="40%" rowspan="7"
<div align="center"
<asp:Image id="Image1" runat="server"></asp:Image
</div
</td
<td width="30%" bgcolor="#e0e0e0"
Listing ID:</td
<td
<asp:Label id="lblListId" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propListID") %>'></asp:Label></td
</tr
<tr
<td bgcolor="#e0e0e0"
Listing Expires:</td
<td
<asp:Label id="lblListExpires" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propListExpires") %>'></asp:Label></td
</tr
<tr
<td bgcolor="#e0e0e0"
BuildingType:</td
<td
<asp:Label id="lblBuildingType" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propBuildType") %>'></asp:Label></td
</tr
<tr
<td bgcolor="#e0e0e0"
Number of Rooms:</td
<td
<asp:Label id="lblRooms" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propRoomNumber") %>'></asp:Label></td
</tr
<tr
<td bgcolor="#e0e0e0"
Rent per Week:</td
<td
<asp:Label id="lblRent" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propRentPerWeek") %>'></asp:Label></td
</tr
<tr
<td bgcolor="#e0e0e0"
City/Town:</td
<td
<asp:Label id="lblCityTown" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propCityTown") %>'></asp:Label></td
</tr
<tr
<td bgcolor="#e0e0e0"
Suburb:</td
<td
<asp:Label id="lblSuburb" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propSuburb") %>'></asp:Label></td
</tr
<tr
<td bgcolor="#e0e0e0" colspan="3"
Description:</td
</tr
<tr
<td colspan="3"
<asp:Label id="lblDescription" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.propDescription") %>'></asp:Label></td
</tr
<tr
<td colspan="2"></td
<td></td
</tr
<tr
<td colspan="2"
<p></p
</td
<td
<div align="right"
<asp:HyperLink id="hypViewListing" runat="server" Font-Size="Medium" NavigateUrl='<%# "fullpropertlisting.aspxID=" & Container.DataItem("propListID") %>'>View Listing</asp:HyperLink
</div
</td
</tr
</tbody
</table
</p
</font></td
<td background="../images/box-orange/right.gif"
<img height="1" src="http://pics.10026.com/?src=../images/box-orange/sizer.gif" width="10" /></td
</tr
<tr
<td
<img height="10" src="http://pics.10026.com/?src=../images/box-orange/bottom-left.gif" width="10" /></td
<td background="../images/box-orange/bottom.gif"
<img height="10" src="http://pics.10026.com/?src=../images/box-orange/sizer.gif" width="1" /></td
<td
<img height="10" src="http://pics.10026.com/?src=../images/box-orange/bottom-right.gif" width="10" /></td
</tr
</tbody
</table
<p></p
</ItemTemplate
<SeparatorTemplate
<table height="10" width="300"
<tbody
<tr
<td></td
</tr
</tbody
</table
</SeparatorTemplate
</asp:DataList
</p
<p
<wmx:SqlDataSourceControl id="dsPropertyLisiting" runat="server" ConnectionString="ConfigurationSettings.AppSettings(" SelectCommand="SELECT propListID, propListExpires, propBuildType, propRoomNumber, propRentPerWeek, propDescription, propCityTown, propSuburb FROM tblPropertyList WHERE propCityTown=@dotnet.itags.org.CityTown, propSuburb=@dotnet.itags.org.Suburb, propRooms=@dotnet.itags.org.Rooms, propRentPerWeek=@dotnet.itags.org.Rent"></wmx:SqlDataSourceControl

</p
</td
</tr
<tr
<td width="30%"
</td
<td
</td
</tr
<tr
<td width="30%"
</td
<td
</td
</tr
</tbody
</table
<p
</p
</form
</body
</html

It actually doesn't have anything to do with parametrized queries.
The problem here is that you're trying to execute code that is outside of the scope of any method.
Remember VB.NET is object-oriented and thus all code needs to be in a method or property accessor.
Copy your code to Page_Load or Page_Init and it should run fine.

0 comments:

Post a Comment