Populate a dropdown list with a variable from a Querystring

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim via DotNetMonster.com

    Populate a dropdown list with a variable from a Querystring

    Hi,

    I'm passing a variable to another page through a querystring. I then want
    to use that variable to retrieve records from a database to poulate a
    dropdownlist. I can read the variable from the querystring but I'm not sure
    how to pass that value. I get the error that IntCourseID is not declared in:

    <asp:DropDownLi st id="fLessonID" runat="server" DataValueField= "LessonID"
    DataSource="<%# GetLessons(IntC ourseID) %>"

    I tried moving the querystring outside the page load but that didn't work:

    This is the code I have:

    <%@ Page Language="VB" Debug="true" validaterequest ="false" %>

    <script runat="server">

    Sub Page_Load(sende r As Object, e As EventArgs)
    If Page.IsPostBack = False Then
    Dim IntCourseID as Integer
    IntCourseID = Request.QuerySt ring( "CourseID" )

    End If
    End Sub


    Function GetLessons(ByVa l courseID As Integer) As
    System.Data.IDa taReader
    Dim connectionStrin g As String = "server='(local )';
    trusted_connect ion=true; database='xx'"
    Dim dbConnection As System.Data.IDb Connection = New
    System.Data.Sql Client.SqlConne ction(connectio nString)

    Dim queryString As String = "SELECT [tblLesson].[LessonID],
    [tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
    "[tblLesson].[CourseID] = @CourseID)"
    Dim dbCommand As System.Data.IDb Command = New
    System.Data.Sql Client.SqlComma nd
    dbCommand.Comma ndText = queryString
    dbCommand.Conne ction = dbConnection

    Dim dbParam_courseI D As System.Data.IDa taParameter = New
    System.Data.Sql Client.SqlParam eter
    dbParam_courseI D.ParameterName = "@CourseID"
    dbParam_courseI D.Value = courseID
    dbParam_courseI D.DbType = System.Data.DbT ype.Int32
    dbCommand.Param eters.Add(dbPar am_courseID)

    dbConnection.Op en
    Dim dataReader As System.Data.IDa taReader =
    dbCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection)

    Return dataReader
    End Function

    Sub addButton_Click (sender As Object, e As EventArgs)
    Response.Redire ct("selectPage. aspx")
    End Sub

    </script>
    <html>
    <head>
    </head>
    <body leftmargin="0" topmargin="0">
    <form runat="server">
    <asp:DropDownLi st id="fLessonID" runat="server"
    DataValueField= "LessonID" DataSource="<%# GetLessons(IntC ourseID) %>"
    DataTextField=" LessonTitle"></asp:DropDownLis t>
    <asp:Button id="addButton" onclick="addBut ton_Click" runat="server"
    Text="Next"></asp:Button>

    </form>
    </body>
    </html>

    Thanks for any help

    --
    Message posted via http://www.dotnetmonster.com
  • DotNetJerome

    #2
    RE: Populate a dropdown list with a variable from a Querystring

    Hi Jim,

    I understood your probleim in the following way after reading your code...

    You have a Integer Variable (IntCourseID) in your code behind fi.e...and you
    want to use that value in your .aspx page. Am I right...?

    If I'm right....then solution for your problem is...

    Have a hidden control...and assign your querystring value (CourseId) to the
    value of the hidden control. Now you can use that control in your .aspx page
    code, to bind with the ddl.

    Cheers,

    Jerome. M



    "Jim via DotNetMonster.c om" wrote:
    [color=blue]
    > Hi,
    >
    > I'm passing a variable to another page through a querystring. I then want
    > to use that variable to retrieve records from a database to poulate a
    > dropdownlist. I can read the variable from the querystring but I'm not sure
    > how to pass that value. I get the error that IntCourseID is not declared in:
    >
    > <asp:DropDownLi st id="fLessonID" runat="server" DataValueField= "LessonID"
    > DataSource="<%# GetLessons(IntC ourseID) %>"
    >
    > I tried moving the querystring outside the page load but that didn't work:
    >
    > This is the code I have:
    >
    > <%@ Page Language="VB" Debug="true" validaterequest ="false" %>
    >
    > <script runat="server">
    >
    > Sub Page_Load(sende r As Object, e As EventArgs)
    > If Page.IsPostBack = False Then
    > Dim IntCourseID as Integer
    > IntCourseID = Request.QuerySt ring( "CourseID" )
    >
    > End If
    > End Sub
    >
    >
    > Function GetLessons(ByVa l courseID As Integer) As
    > System.Data.IDa taReader
    > Dim connectionStrin g As String = "server='(local )';
    > trusted_connect ion=true; database='xx'"
    > Dim dbConnection As System.Data.IDb Connection = New
    > System.Data.Sql Client.SqlConne ction(connectio nString)
    >
    > Dim queryString As String = "SELECT [tblLesson].[LessonID],
    > [tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
    > "[tblLesson].[CourseID] = @CourseID)"
    > Dim dbCommand As System.Data.IDb Command = New
    > System.Data.Sql Client.SqlComma nd
    > dbCommand.Comma ndText = queryString
    > dbCommand.Conne ction = dbConnection
    >
    > Dim dbParam_courseI D As System.Data.IDa taParameter = New
    > System.Data.Sql Client.SqlParam eter
    > dbParam_courseI D.ParameterName = "@CourseID"
    > dbParam_courseI D.Value = courseID
    > dbParam_courseI D.DbType = System.Data.DbT ype.Int32
    > dbCommand.Param eters.Add(dbPar am_courseID)
    >
    > dbConnection.Op en
    > Dim dataReader As System.Data.IDa taReader =
    > dbCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection)
    >
    > Return dataReader
    > End Function
    >
    > Sub addButton_Click (sender As Object, e As EventArgs)
    > Response.Redire ct("selectPage. aspx")
    > End Sub
    >
    > </script>
    > <html>
    > <head>
    > </head>
    > <body leftmargin="0" topmargin="0">
    > <form runat="server">
    > <asp:DropDownLi st id="fLessonID" runat="server"
    > DataValueField= "LessonID" DataSource="<%# GetLessons(IntC ourseID) %>"
    > DataTextField=" LessonTitle"></asp:DropDownLis t>
    > <asp:Button id="addButton" onclick="addBut ton_Click" runat="server"
    > Text="Next"></asp:Button>
    >
    > </form>
    > </body>
    > </html>
    >
    > Thanks for any help
    >
    > --
    > Message posted via http://www.dotnetmonster.com
    >[/color]

    Comment

    • Malik Asif Joyia

      #3
      Re: Populate a dropdown list with a variable from a Querystring

      Hello
      Dear Jim!
      First the problem with your code is that you are declaring a Variable in
      pageload event "IntCourseI D" that will be destroyed when this event will
      reach its end.
      First Declare this variable in the GetLessons function
      IntCourseID = Request.QuerySt ring( "CourseID" )
      so that this variable with its value can be accessed.

      Regards
      Malik Asif
      ASP.NET,VB.NET


      "Jim via DotNetMonster.c om" <forum@DotNetMo nster.com> wrote in message
      news:8596ac9543 c84e2e8aad88fcc 163c277@DotNetM onster.com...[color=blue]
      > Hi,
      >
      > I'm passing a variable to another page through a querystring. I then want
      > to use that variable to retrieve records from a database to poulate a
      > dropdownlist. I can read the variable from the querystring but I'm not[/color]
      sure[color=blue]
      > how to pass that value. I get the error that IntCourseID is not declared[/color]
      in:[color=blue]
      >
      > <asp:DropDownLi st id="fLessonID" runat="server" DataValueField= "LessonID"
      > DataSource="<%# GetLessons(IntC ourseID) %>"
      >
      > I tried moving the querystring outside the page load but that didn't work:
      >
      > This is the code I have:
      >
      > <%@ Page Language="VB" Debug="true" validaterequest ="false" %>
      >
      > <script runat="server">
      >
      > Sub Page_Load(sende r As Object, e As EventArgs)
      > If Page.IsPostBack = False Then
      > Dim IntCourseID as Integer
      > IntCourseID = Request.QuerySt ring( "CourseID" )
      >
      > End If
      > End Sub
      >
      >
      > Function GetLessons(ByVa l courseID As Integer) As
      > System.Data.IDa taReader
      > Dim connectionStrin g As String = "server='(local )';
      > trusted_connect ion=true; database='xx'"
      > Dim dbConnection As System.Data.IDb Connection = New
      > System.Data.Sql Client.SqlConne ction(connectio nString)
      >
      > Dim queryString As String = "SELECT [tblLesson].[LessonID],
      > [tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
      > "[tblLesson].[CourseID] = @CourseID)"
      > Dim dbCommand As System.Data.IDb Command = New
      > System.Data.Sql Client.SqlComma nd
      > dbCommand.Comma ndText = queryString
      > dbCommand.Conne ction = dbConnection
      >
      > Dim dbParam_courseI D As System.Data.IDa taParameter = New
      > System.Data.Sql Client.SqlParam eter
      > dbParam_courseI D.ParameterName = "@CourseID"
      > dbParam_courseI D.Value = courseID
      > dbParam_courseI D.DbType = System.Data.DbT ype.Int32
      > dbCommand.Param eters.Add(dbPar am_courseID)
      >
      > dbConnection.Op en
      > Dim dataReader As System.Data.IDa taReader =
      > dbCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection)
      >
      > Return dataReader
      > End Function
      >
      > Sub addButton_Click (sender As Object, e As EventArgs)
      > Response.Redire ct("selectPage. aspx")
      > End Sub
      >
      > </script>
      > <html>
      > <head>
      > </head>
      > <body leftmargin="0" topmargin="0">
      > <form runat="server">
      > <asp:DropDownLi st id="fLessonID" runat="server"
      > DataValueField= "LessonID" DataSource="<%# GetLessons(IntC ourseID) %>"
      > DataTextField=" LessonTitle"></asp:DropDownLis t>
      > <asp:Button id="addButton" onclick="addBut ton_Click"[/color]
      runat="server"[color=blue]
      > Text="Next"></asp:Button>
      >
      > </form>
      > </body>
      > </html>
      >
      > Thanks for any help
      >
      > --
      > Message posted via http://www.dotnetmonster.com[/color]


      Comment

      Working...