Variable Definition...

M

Murph

Hi,

I'm new to ASP.NET and have a problem which i'm hoping will be easy to
answer... i have only used VB, HTML and Flash up until now!

I am designing an ASP page which uses OleDb to retrieve data from an
access database and show it on screen... however, i want the viewer
to be able to select an item from a dropdown menu which relates to
one of the fields and have only that information shown... how do i
declare the variable?

Here is my code:

<%@ Import
Namespace="System.Data.OleDb" %>

<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data
source=" &
server.mappath("gigs.mdb"))
dbconn.Open()
sql="SELECT * FROM gigs"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
gigs.DataSource=dbread
gigs.DataBind()
dbread.Close()
dbconn.Close()
end sub

sub getvenue_click (Sender As Object, E As EventArgs)
dim dbconn,sql,dbcomm,dbread
dim myselection
dbconn=New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data
source=" &
server.mappath("gigs.mdb"))
dbconn.Open()
sql="SELECT * FROM gigs where venue = ???"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
gigs.DataSource=dbread
gigs.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>

<html>
<body>
<select id="MyDDL" runat="server">
<option>The Brickyard</option>
<option>Whitehaven Civic Hall</option>
<option>Kendal Rugby Club</option>
</select>
<br><br>
<input type="button"
OnServerClick="getvenue_click" value="Show
Results" runat="server">
<br><br>
<form runat="server">
<asp:Repeater id="gigs" runat="server">

<HeaderTemplate>
<!--<table width="100%">
<tr bgcolor="#AA0000">
<th><font face="tahoma" size=2
color="#FFFFFF">Date</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Line-Up</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Venue</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Contact</th>
</tr>-->
</HeaderTemplate>

<ItemTemplate>
<table width="100%">
<tr bgcolor="#FFFFFF">
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><b><%#Container.DataItem("date")%></b><br><font
size=1><%#Container.DataItem("doorinfo")%></td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><b><%#Container.DataItem("promoter")%></b><br><%#Container.DataItem("band1")%><br><%#Container.DataItem("band2")%><br><%#Container.DataItem("band3")%><br><%#Container.DataItem("band4")%>
</td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><%#Container.DataItem("venue")%><br><font
size=1><%#Container.DataItem("venueadd")%>
</td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><%#Container.DataItem("contact")%>
</td>
</tr>
</table>
<hr color="#E9BEBE">
</ItemTemplate>

<AlternatingItemTemplate>
<table width="100%">
<tr bgcolor="#FFFFFF">
<td width="25%"><font face="tahoma"
size=2><b><%#Container.DataItem("date")%></b><br><font
size=1><%#Container.DataItem("doorinfo")%></td>
<td width="25%"><font face="tahoma"
size=2><b><%#Container.DataItem("promoter")%></b><br><%#Container.DataItem("band1")%><br><%#Container.DataItem("band2")%><br><%#Container.DataItem("band3")%><br><%#Container.DataItem("band4")%>
</td>
<td width="25%"><font face="tahoma"
size=2><%#Container.DataItem("venue")%><br><font
size=1><%#Container.DataItem("venueadd")%>
</td>
<td width="25%"><font face="tahoma"
size=2><%#Container.DataItem("contact")%>
</td>
</tr>
</table>
<hr color="#E9BEBE">
</AlternatingItemTemplate>

<FooterTemplate>
<!--</table>-->
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

My problem is... in the sub getvenue_click, i have a line saying
SELECT * From gigs where venue = ???, say i put something like
"myselection" in place of ??? How do i define the variable
"myselection"?

Eg. In Visual Basic i would say

dim myvariable
myvariable = MyDDL.Value

i basically need to know the equivelant to that in ASP.

Sorry this post is so long but i thought it might make more sense to
someone if i showed the code for the whole page.

Any help would be appreciated!

Thanks,

Murph
 
C

Curt_C [MVP]

depends on if the ddl is an asp:dropdownlist or a <select>
if it's the first....
string varName = this.ddlName.SelectedItem.Value.ToString();

Otherwise,
string varName = Request.Form["ddlName"].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com


Murph said:
Hi,

I'm new to ASP.NET and have a problem which i'm hoping will be easy to
answer... i have only used VB, HTML and Flash up until now!

I am designing an ASP page which uses OleDb to retrieve data from an
access database and show it on screen... however, i want the viewer
to be able to select an item from a dropdown menu which relates to
one of the fields and have only that information shown... how do i
declare the variable?

Here is my code:

<%@ Import
Namespace="System.Data.OleDb" %>

<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data
source=" &
server.mappath("gigs.mdb"))
dbconn.Open()
sql="SELECT * FROM gigs"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
gigs.DataSource=dbread
gigs.DataBind()
dbread.Close()
dbconn.Close()
end sub

sub getvenue_click (Sender As Object, E As EventArgs)
dim dbconn,sql,dbcomm,dbread
dim myselection
dbconn=New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data
source=" &
server.mappath("gigs.mdb"))
dbconn.Open()
sql="SELECT * FROM gigs where venue = ???"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
gigs.DataSource=dbread
gigs.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>

<html>
<body>
<select id="MyDDL" runat="server">
<option>The Brickyard</option>
<option>Whitehaven Civic Hall</option>
<option>Kendal Rugby Club</option>
</select>
<br><br>
<input type="button"
OnServerClick="getvenue_click" value="Show
Results" runat="server">
<br><br>
<form runat="server">
<asp:Repeater id="gigs" runat="server">

<HeaderTemplate>
<!--<table width="100%">
<tr bgcolor="#AA0000">
<th><font face="tahoma" size=2
color="#FFFFFF">Date</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Line-Up</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Venue</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Contact</th>
</tr>-->
</HeaderTemplate>

<ItemTemplate>
<table width="100%">
<tr bgcolor="#FFFFFF">
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><b><%#Container.DataItem("date")%></b><br><font
size=1><%#Container.DataItem("doorinfo")%></td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><b><%#Container.DataItem("promoter")%></b><br><%#Container.DataItem("
band1")%> said:
</td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><%#Container.DataItem("venue")%><br><font
size=1><%#Container.DataItem("venueadd")%>
</td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><%#Container.DataItem("contact")%>
</td>
</tr>
</table>
<hr color="#E9BEBE">
</ItemTemplate>

<AlternatingItemTemplate>
<table width="100%">
<tr bgcolor="#FFFFFF">
<td width="25%"><font face="tahoma"
size=2><b><%#Container.DataItem("date")%></b><br><font
size=1><%#Container.DataItem("doorinfo")%></td>
<td width="25%"><font face="tahoma"
size=2><b><%#Container.DataItem("promoter")%></b><br><%#Container.DataItem("
band1")%> said:
</td>
<td width="25%"><font face="tahoma"
size=2><%#Container.DataItem("venue")%><br><font
size=1><%#Container.DataItem("venueadd")%>
</td>
<td width="25%"><font face="tahoma"
size=2><%#Container.DataItem("contact")%>
</td>
</tr>
</table>
<hr color="#E9BEBE">
</AlternatingItemTemplate>

<FooterTemplate>
<!--</table>-->
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

My problem is... in the sub getvenue_click, i have a line saying
SELECT * From gigs where venue = ???, say i put something like
"myselection" in place of ??? How do i define the variable
"myselection"?

Eg. In Visual Basic i would say

dim myvariable
myvariable = MyDDL.Value

i basically need to know the equivelant to that in ASP.

Sorry this post is so long but i thought it might make more sense to
someone if i showed the code for the whole page.

Any help would be appreciated!

Thanks,

Murph
 
M

Mohammad Samara

Create an SQL parameter, i.e:

da.SelectCommand.Parameters["@varname"].Value = DropDownList.SelectedItem;

Don't forget to add the variable to your select statement:...."select
whatever from table where field=@varname"....

And of course add the parameter to your selectcommand parameters:

da.SelectCommand.Parameters.Add(new
System.Data.OracleClient.OracleParameter("@varname",
System.Data.OracleClient.OracleType.VarChar, 20, "field"));

the above example is for oracle db, replace oracle variables with your oledb
ones.

m.
------------
Murph said:
Hi,

I'm new to ASP.NET and have a problem which i'm hoping will be easy to
answer... i have only used VB, HTML and Flash up until now!

I am designing an ASP page which uses OleDb to retrieve data from an
access database and show it on screen... however, i want the viewer
to be able to select an item from a dropdown menu which relates to
one of the fields and have only that information shown... how do i
declare the variable?

Here is my code:

<%@ Import
Namespace="System.Data.OleDb" %>

<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data
source=" &
server.mappath("gigs.mdb"))
dbconn.Open()
sql="SELECT * FROM gigs"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
gigs.DataSource=dbread
gigs.DataBind()
dbread.Close()
dbconn.Close()
end sub

sub getvenue_click (Sender As Object, E As EventArgs)
dim dbconn,sql,dbcomm,dbread
dim myselection
dbconn=New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data
source=" &
server.mappath("gigs.mdb"))
dbconn.Open()
sql="SELECT * FROM gigs where venue = ???"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
gigs.DataSource=dbread
gigs.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>

<html>
<body>
<select id="MyDDL" runat="server">
<option>The Brickyard</option>
<option>Whitehaven Civic Hall</option>
<option>Kendal Rugby Club</option>
</select>
<br><br>
<input type="button"
OnServerClick="getvenue_click" value="Show
Results" runat="server">
<br><br>
<form runat="server">
<asp:Repeater id="gigs" runat="server">

<HeaderTemplate>
<!--<table width="100%">
<tr bgcolor="#AA0000">
<th><font face="tahoma" size=2
color="#FFFFFF">Date</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Line-Up</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Venue</th>
<th><font face="tahoma" size=2
color="#FFFFFF">Contact</th>
</tr>-->
</HeaderTemplate>

<ItemTemplate>
<table width="100%">
<tr bgcolor="#FFFFFF">
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><b><%#Container.DataItem("date")%></b><br><font
size=1><%#Container.DataItem("doorinfo")%></td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><b><%#Container.DataItem("promoter")%></b><br><%#Container.DataItem("
band1")%> said:
</td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><%#Container.DataItem("venue")%><br><font
size=1><%#Container.DataItem("venueadd")%>
</td>
<td width="25%"><font face="tahoma"
color="#AA0000"
size=2><%#Container.DataItem("contact")%>
</td>
</tr>
</table>
<hr color="#E9BEBE">
</ItemTemplate>

<AlternatingItemTemplate>
<table width="100%">
<tr bgcolor="#FFFFFF">
<td width="25%"><font face="tahoma"
size=2><b><%#Container.DataItem("date")%></b><br><font
size=1><%#Container.DataItem("doorinfo")%></td>
<td width="25%"><font face="tahoma"
size=2><b><%#Container.DataItem("promoter")%></b><br><%#Container.DataItem("
band1")%> said:
</td>
<td width="25%"><font face="tahoma"
size=2><%#Container.DataItem("venue")%><br><font
size=1><%#Container.DataItem("venueadd")%>
</td>
<td width="25%"><font face="tahoma"
size=2><%#Container.DataItem("contact")%>
</td>
</tr>
</table>
<hr color="#E9BEBE">
</AlternatingItemTemplate>

<FooterTemplate>
<!--</table>-->
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

My problem is... in the sub getvenue_click, i have a line saying
SELECT * From gigs where venue = ???, say i put something like
"myselection" in place of ??? How do i define the variable
"myselection"?

Eg. In Visual Basic i would say

dim myvariable
myvariable = MyDDL.Value

i basically need to know the equivelant to that in ASP.

Sorry this post is so long but i thought it might make more sense to
someone if i showed the code for the whole page.

Any help would be appreciated!

Thanks,

Murph
 
M

Murph

Curt_C [MVP]wrote:
]depends on if the ddl is an asp:dropdownlist or a said:
if it's the first....
string varName = this.ddlName.SelectedItem.Value.ToString();

Otherwise,
string varName = Request.Form["ddlName"].ToString();

I am using a <select> ddl.

so i would code as:

sql = "SELECT * From gigs where venue = @venue"
string varName = Request.Form["MyDDL"].ToString(@venue);

Is this correct?
 
M

Murph

I can only assume that's not what you meant as now when i try to open
the .aspx page it comes up with "Compilation Error"

Compiler Error Message: BC30109: 'String' is a class type, and so is
not a valid expression.

Source Error:



Line 21: dbconn.Open()
Line 22: sql="SELECT * FROM gigs where venue = @venue"Request.Form["MyDDL"].ToString(@venue);
Line 24: dbcomm=New OleDbCommand(sql,dbconn)
Line 25: dbread=dbcomm.ExecuteReader()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top