Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

DropDownList problem

Hello,

I'm trying to retrieve data from a DropDownList to display in a GridView below. I'm trying to retrieve 3 fields total, one of those fields i need to be able to download a file from the location specified. Below is my current code.

default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="pntr_bldgid" HeaderText="Printer ID" />
                <asp:BoundField DataField="pntr_bldgname" HeaderText="Printer Name" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


default.aspx.vb

Imports System.Data
Imports System.Data.SqlTypes
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Security

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Printer").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Dim pntr_bldgid As Int32 = Request.QueryString("@pntr_bldgid")

        Dim gridviewComm As SqlCommand
        gridviewComm = New SqlCommand("GridViewTest", conn)
        gridviewComm.CommandType = CommandType.StoredProcedure

        ' Create Parameter for Querystring
        gridviewComm.Parameters.Add("@pntr_bldgid", SqlDbType.Int)
        gridviewComm.Parameters("@pntr_bldgid").Value = DropDownList1.SelectedValue

        Dim adapter As SqlDataAdapter

        Dim dtGridview As DataTable = New DataTable()

        Try
            conn.Open()

            adapter = New SqlDataAdapter
            adapter.SelectCommand = gridviewComm
            adapter.Fill(dtGridview)

            GridView1.DataSource = dtGridview
            GridView1.DataBind()

        Catch ex As Exception

        Finally
            conn.Close()
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Printer").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Dim printerComm As SqlCommand
        printerComm = New SqlCommand("PrinterTest", conn)
        printerComm.CommandType = CommandType.StoredProcedure

        Dim adapter As SqlDataAdapter

        Dim dtPrinters As DataTable = New DataTable()

        Try
            conn.Open()

            adapter = New SqlDataAdapter
            adapter.SelectCommand = printerComm
            adapter.Fill(dtPrinters)

            DropDownList1.DataSource = dtPrinters
            DropDownList1.DataValueField = "pntr_bldgid"
            DropDownList1.DataTextField = "pntr_bldgname"
            DropDownList1.DataBind()

        Catch ex As Exception

        Finally
            conn.Close()
        End Try
    End Sub
End Class

PrinterTest SP:

ALTER PROCEDURE [dbo].[PrinterTest]

as

Select pntr_bldgid, pntr_bldgname, pntr_bldgloc, pntr_bldgbatfile
from PRINTER_BLDG
order by pntr_bldgname asc

GridViewTest SP:


ALTER PROCEDURE [dbo].[GridViewTest]

@pntr_bldgid int

as

Select pntr_bldgid, pntr_bldgname, pntr_bldgloc, pntr_bldgbatfile
from PRINTER_BLDG
where pntr_bldgid = @pntr_bldgid
Avatar of David Robitaille
David Robitaille
Flag of Canada image

What is your question exactly?
 
Avatar of Brian

ASKER

When i select a value from the DropDownList i would like to populate the GridView with my selection from the DropDownList but i can't get it to work :(
first, try  to add  in your page load event  a "If Not (Page.IsPostBack) Then" because the way you set it, your DropDownList1 "rebind" at each post back. probaly reseting it.
 
Avatar of Brian

ASKER

still no dice!
it it the command that dont work or your DropDownList1.SelectedValue
test:
Button1.text =  DropDownList1.SelectedValue
 
note, another way of doing this is using a SQLDatasource and a commmand parameter.
you could also fill the dropdonw using a SQLDatasource and a QuerySting parameter.

 
Avatar of Brian

ASKER

If i add a Button control without a Button Event Handler then when i run the page and select a value from DDL and hit the button then it does display my data in GridView but i would like this to work without a button.
ASKER CERTIFIED SOLUTION
Avatar of David Robitaille
David Robitaille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Thank you davrob60!! That worked as i needed.