Using Different Parameter Types with the ObjectDataSource control - Demo
You can use all of the same types of parameters with the ObjectDataSource control
that you can use with the SqlDataSource control.
- Parameter - An arbitrary static value
- ControlParameter - The value of a control or page property (used in this example)
- CookieParameter - The value of a browser cookie
- FormParameter - The value of a HTML form field
- ProfileParameter - The value of a Profile property
- QueryStringParameter - The value of a query string field
- SessionParameter - The value of an item stored in Session state
In this example, The SelectParameters collection contains a ControlParameter which
represents the current value of the ddlMovieCategory Dropdownlist control.
*** MovieCategories.vb ***
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Public Class MovieCategories
Private ReadOnly _conString As String
Public Function GetMovies(ByVal categoryId As Integer) As SqlDataReader
' Create Connection
Dim con As New SqlConnection(_conString)
' Create Command
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandText = "SELECT Id,Title,Director,DateReleased " _
+ " FROM Movies WHERE CategoryId=@CategoryId"
' Add parameters
cmd.Parameters.AddWithValue("@CategoryId", categoryId)
' Return DataReader
con.Open()
Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
End Function
Public Function GetCategories() As SqlDataReader
' Create Connection
Dim con As New SqlConnection(_conString)
' Create Command
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = con
cmd.CommandText = "SELECT Id,Name FROM MovieCategories"
' Return DataReader
con.Open()
Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
End Function
Public Sub New()
_conString = WebConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString
End Sub
End Class
*** ShowMoviesByCategory.aspx ***
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<style type="text/css">
.movies
{
border:Solid 1px black;
}
.movies td,.movies th
{
padding:5px;
}
</style>
<title>Show Movies by Category</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList
id="ddlMovieCategory"
DataSourceID="srcMovieCategories"
DataTextField="Name"
DataValueField="Id"
ToolTip="Movie Category"
Runat="server" />
<asp:Button
id="btnSelect"
Text="Select"
Runat="server" />
<asp:GridView
id="grdMovies"
DataSourceID="srcMovies"
CssClass="movies"
GridLines="None"
Runat="server" />
<asp:ObjectDataSource
id="srcMovieCategories"
TypeName="MovieCategories"
SelectMethod="GetCategories"
Runat="server" />
<asp:ObjectDataSource
id="srcMovies"
TypeName="MovieCategories"
SelectMethod="GetMovies"
Runat="server">
<SelectParameters>
<asp:ControlParameter
Name="CategoryId"
ControlID="ddlMovieCategory" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>