Monday, March 26, 2012

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:
<%@dotnet.itags.org. 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(connectionString)
Dim queryString As String = "SELECT [tblLesson].[LessonID],
[tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
"[tblLesson].[CourseID] = @dotnet.itags.org.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 = "@dotnet.itags.org.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.CommandBehavior.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.webservertalk.comHi 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 webservertalk.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 sur
e
> how to pass that value. I get the error that IntCourseID is not declared i
n:
> <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(connectionString)
> 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.CommandBehavior.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.webservertalk.com
>
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 webservertalk.com" <forum@.webservertalk.com> wrote in message
news:8596ac9543c84e2e8aad88fcc163c277@.Do
webservertalk.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(connectionString)
> 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.CommandBehavior.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.webservertalk.com

0 comments:

Post a Comment