Inserting a variable into the selectparameters of a SQL command

Discussion in 'ASP .Net' started by evandela@bigpond.net.au, Mar 22, 2006.

  1. Guest

    Hi evreyone, I am a newbie but my ASP.net is comming on thanks to you
    guys and the countless manuals I have downloaded... ummm... I mean
    'bought'

    I am having a little bit of an issue though, I am sure it is easy to
    sort out - but I cant seem to find any info on it.

    I have a variable that I would like to insert into the SQL code... but
    cant seem to figure out how to do it... this is what my code looks like
    right now... if I replace the ? with a product number the SQL query
    works.... but I have a variable called SelectedProductID and I need to
    find out how to get that into the SQL query :(


    Dim SelectedProductID as Int32 = 4

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs)
    AvaliableColours.SelectCommand = "SELECT colours.colourID as
    ALLColourID, colours.colour, avaliableColours.productID,
    avaliableColours.colourID FROM colours, avaliableColours WHERE
    colours.colourID = avaliableColours.colourID AND
    avaliableColours.productID=?"
    End Sub

    <asp:AccessDataSource ID="AvaliableColours" runat="server"
    DataFile="~/App_Data/products.mdb" />

    Any ideas ?
    , Mar 22, 2006
    #1
    1. Advertising

  2. If SelectedProductID is numeric value then use:
    AvaliableColours.SelectCommand = "SELECT colours.colourID as ALLColourID,
    colours.colour, avaliableColours.productID, avaliableColours.colourID FROM
    colours, avaliableColours WHERE colours.colourID = avaliableColours.colourID
    AND avaliableColours.productID= " & SelectedProductID

    If it is string value then use:
    AvaliableColours.SelectCommand = "SELECT colours.colourID as ALLColourID,
    colours.colour, avaliableColours.productID, avaliableColours.colourID FROM
    colours, avaliableColours WHERE colours.colourID = avaliableColours.colourID
    AND avaliableColours.productID= '" & SelectedProductID & "'"

    Hope this helps,
    VHD50.


    "" wrote:

    > Hi evreyone, I am a newbie but my ASP.net is comming on thanks to you
    > guys and the countless manuals I have downloaded... ummm... I mean
    > 'bought'
    >
    > I am having a little bit of an issue though, I am sure it is easy to
    > sort out - but I cant seem to find any info on it.
    >
    > I have a variable that I would like to insert into the SQL code... but
    > cant seem to figure out how to do it... this is what my code looks like
    > right now... if I replace the ? with a product number the SQL query
    > works.... but I have a variable called SelectedProductID and I need to
    > find out how to get that into the SQL query :(
    >
    >
    > Dim SelectedProductID as Int32 = 4
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > AvaliableColours.SelectCommand = "SELECT colours.colourID as
    > ALLColourID, colours.colour, avaliableColours.productID,
    > avaliableColours.colourID FROM colours, avaliableColours WHERE
    > colours.colourID = avaliableColours.colourID AND
    > avaliableColours.productID=?"
    > End Sub
    >
    > <asp:AccessDataSource ID="AvaliableColours" runat="server"
    > DataFile="~/App_Data/products.mdb" />
    >
    > Any ideas ?
    >
    >
    =?Utf-8?B?VkhENTA=?=, Mar 22, 2006
    #2
    1. Advertising

  3. gella Guest

    try this ....it should work

    "SELECT colours.colourID as
    ALLColourID, colours.colour, avaliableColours.productID,
    avaliableColours.colourID FROM colours, avaliableColours WHERE
    colours.colourID = avaliableColours.colourID AND
    avaliableColours.productID=" & SelectedProductID &" "
    gella, Mar 29, 2006
    #3
    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. John Smith
    Replies:
    1
    Views:
    1,227
    Brock Allen
    Jun 17, 2005
  2. Mike P

    GridView SelectParameters

    Mike P, May 30, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    23,656
    Brock Allen
    Jun 1, 2006
  3. MikeB
    Replies:
    2
    Views:
    1,501
    MikeB
    Apr 5, 2007
  4. ray.ragan

    Difficulties with Selectparameters

    ray.ragan, Sep 6, 2007, in forum: .NET
    Replies:
    1
    Views:
    417
    ray.ragan
    Sep 6, 2007
  5. jobs
    Replies:
    0
    Views:
    490
Loading...

Share This Page