Variable Definition...

Discussion in 'ASP .Net' started by Murph, Jan 14, 2004.

  1. Murph

    Murph Guest

    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


    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    Murph, Jan 14, 2004
    #1
    1. Advertising

  2. Murph

    Curt_C [MVP] Guest

    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" <-spam.invalid> wrote in message
    news:4005a447$...
    > 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("band
    3")%><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("band
    3")%><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
    >
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
     
    Curt_C [MVP], Jan 14, 2004
    #2
    1. Advertising

  3. 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" <-spam.invalid> wrote in message
    news:4005a447$...
    > 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("band
    3")%><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("band
    3")%><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
    >
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
     
    Mohammad Samara, Jan 14, 2004
    #3
  4. Murph

    Murph Guest

    > Curt_C [MVP]wrote:
    "]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();
    >
    >


    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?


    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    Murph, Jan 14, 2004
    #4
  5. Murph

    Murph Guest

    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"
    >> Line 23: string varName =

    Request.Form["MyDDL"].ToString(@venue);
    Line 24: dbcomm=New OleDbCommand(sql,dbconn)
    Line 25: dbread=dbcomm.ExecuteReader()

    >> is the line which is highlighted red on the page!



    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    Murph, Jan 14, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    598
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    426
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    454
    Jon Slaughter
    Oct 26, 2005
  4. mfglinux
    Replies:
    11
    Views:
    712
    Roberto Bonvallet
    Sep 12, 2007
  5. Pierre Yves
    Replies:
    2
    Views:
    495
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page