Torna al Thread
<%@ Page Language="VB" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' If Not Page.IsPostBack Then
Dim Where As Object
Where = ""
Dim MyReader As SqlDataReader
Dim CityParam As SqlParameter
Dim ContactParam As SqlParameter
Dim MyConnection As SqlConnection = New SqlConnection()
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("NorthwindConnectionString2").ConnectionString
Dim MyCommand As SqlCommand = New SqlCommand()
MyCommand.CommandText = "SELECT * FROM CUSTOMERS "
MyCommand.CommandType = CommandType.Text
MyCommand.Connection = MyConnection
CityParam = New SqlParameter()
CityParam.ParameterName = "@CITY"
CityParam.SqlDbType = SqlDbType.VarChar
CityParam.Size = 15
CityParam.Direction = ParameterDirection.Input
CityParam.Value = DropDownList1.SelectedValue.ToString
ContactParam = New SqlParameter()
ContactParam.ParameterName = "@CONTACT"
ContactParam.SqlDbType = SqlDbType.VarChar
ContactParam.Size = 15
ContactParam.Direction = ParameterDirection.Input
ContactParam.Value = DropDownList2.SelectedValue.ToString
If CityParam.Value.ToString <> "nessuna selezione" Then
MyCommand.CommandText = "SELECT * FROM CUSTOMERS WHERE CITY = @CITY "
End If
If ContactParam.Value.ToString <> "nessuna selezione" Then
If CityParam.Value.ToString <> "nessuna selezione" Then
MyCommand.CommandText = " SELECT * FROM CUSTOMERS WHERE CITY = @CITY AND CONTACTNAME = @CONTACT"
Else
MyCommand.CommandText = " SELECT * FROM CUSTOMERS WHERE CONTACTNAME = @CONTACT"
End If
End If
MyCommand.Parameters.Add(CityParam)
MyCommand.Parameters.Add(ContactParam)
MyCommand.Connection.Open()
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
gvCustomers.DataSource = MyReader
gvCustomers.DataBind()
MyCommand.Dispose()
MyConnection.Dispose()
' Debug.WriteLine(MyCommand.CommandText)
TextBox1.Text = MyCommand.CommandText
' End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="City" DataValueField="City"
AutoPostBack="True" AppendDataBoundItems="true"
>
<asp:ListItem>nessuna selezione</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource2" DataTextField="ContactName"
DataValueField="ContactName" AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem>nessuna selezione</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" Width="551px"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>"
SelectCommand="SELECT DISTINCT [ContactName] FROM [Customers]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>"
SelectCommand="SELECT DISTINCT [City] FROM [Customers]"></asp:SqlDataSource>
<asp:GridView ID="gvCustomers" runat="server">
</asp:GridView>
</form>
</body>
</html>