473,480 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
DataSource="<%# GetLessons(IntCourseID) %>"

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(sender As Object, e As EventArgs)
If Page.IsPostBack = False Then
Dim IntCourseID as Integer
IntCourseID = Request.QueryString( "CourseID" )

End If
End Sub
Function GetLessons(ByVal courseID As Integer) As
System.Data.IDataReader
Dim connectionString As String = "server='(local)';
trusted_connection=true; database='xx'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)

Dim queryString As String = "SELECT [tblLesson].[LessonID],
[tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
"[tblLesson].[CourseID] = @CourseID)"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_courseID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_courseID.ParameterName = "@CourseID"
dbParam_courseID.Value = courseID
dbParam_courseID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_courseID)

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

Return dataReader
End Function

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

</script>
<html>
<head>
</head>
<body leftmargin="0" topmargin="0">
<form runat="server">
<asp:DropDownList id="fLessonID" runat="server"
DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
DataTextField="LessonTitle"></asp:DropDownList>
<asp:Button id="addButton" onclick="addButton_Click" runat="server"
Text="Next"></asp:Button>

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

Thanks for any help

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #1
2 4008
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.com" wrote:
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:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
DataSource="<%# GetLessons(IntCourseID) %>"

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(sender As Object, e As EventArgs)
If Page.IsPostBack = False Then
Dim IntCourseID as Integer
IntCourseID = Request.QueryString( "CourseID" )

End If
End Sub
Function GetLessons(ByVal courseID As Integer) As
System.Data.IDataReader
Dim connectionString As String = "server='(local)';
trusted_connection=true; database='xx'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)

Dim queryString As String = "SELECT [tblLesson].[LessonID],
[tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
"[tblLesson].[CourseID] = @CourseID)"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_courseID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_courseID.ParameterName = "@CourseID"
dbParam_courseID.Value = courseID
dbParam_courseID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_courseID)

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

Return dataReader
End Function

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

</script>
<html>
<head>
</head>
<body leftmargin="0" topmargin="0">
<form runat="server">
<asp:DropDownList id="fLessonID" runat="server"
DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
DataTextField="LessonTitle"></asp:DropDownList>
<asp:Button id="addButton" onclick="addButton_Click" runat="server"
Text="Next"></asp:Button>

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

Thanks for any help

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

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

Regards
Malik Asif
ASP.NET,VB.NET
"Jim via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in message
news:85******************************@DotNetMonste r.com...
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:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
DataSource="<%# GetLessons(IntCourseID) %>"

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(sender As Object, e As EventArgs)
If Page.IsPostBack = False Then
Dim IntCourseID as Integer
IntCourseID = Request.QueryString( "CourseID" )

End If
End Sub
Function GetLessons(ByVal courseID As Integer) As
System.Data.IDataReader
Dim connectionString As String = "server='(local)';
trusted_connection=true; database='xx'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)

Dim queryString As String = "SELECT [tblLesson].[LessonID],
[tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
"[tblLesson].[CourseID] = @CourseID)"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_courseID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_courseID.ParameterName = "@CourseID"
dbParam_courseID.Value = courseID
dbParam_courseID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_courseID)

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

Return dataReader
End Function

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

</script>
<html>
<head>
</head>
<body leftmargin="0" topmargin="0">
<form runat="server">
<asp:DropDownList id="fLessonID" runat="server"
DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
DataTextField="LessonTitle"></asp:DropDownList>
<asp:Button id="addButton" onclick="addButton_Click" runat="server" Text="Next"></asp:Button>

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

Thanks for any help

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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
10185
by: Lisa | last post by:
I have a drop down that defaults to "select" after the page refreshes. How do I keep the selected value in the dropdown field... I've tried EVERYTHING and nothing works! :( <script...
20
12349
by: Dannyboyo | last post by:
I have what I hope is a simple request. I can't really code in javascript, but I am pretty good at cusomizing it with slight modifications. I code in ASP and HTML. I am trying to capture customer...
1
2464
by: news | last post by:
Hello, I need help with the following problem: An HTML page that have two dropdown list whit different values: 1. Dropdown list 1(tblname1) =values with different location's. After user...
2
3018
by: jeffmagill | last post by:
Hello everyone, I have what I thought was a simple script but I cant figure it out. Anyway, I want to set the selected item of a drop down list with information i am recieving from...
5
11860
by: jung_h_park | last post by:
From: jung_h_park@yahoo.com Newsgroups: microsoft.public.dotnet.framework.aspnet Subject: Dropdown List not retaining its SelectedValue Date: Mon, 26 Jun 2006 21:02:57 -0700 Hello, My...
0
1597
by: Kay O'Keeffe | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
11
7353
by: eureka | last post by:
Hi All, I'm training in Servlets, JSP and JavaScript, I have a web page in which there's a "StudentName" textbox and below it is a "Names" Dropdown list. Initially the Textbox is empty and...
6
5835
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
1
1842
by: fran7 | last post by:
Hi, Anyone know how to populate a dropdown list with data from database. I have an interface that generates the following code for names in my database. ..asp <%If Trim(rsCard("Author"))<>""...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.