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
LVL 4
asp_net2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David RobitailleAnalyst ProgrammerCommented:
What is your question exactly?
 
0
asp_net2Author Commented:
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 :(
0
David RobitailleAnalyst ProgrammerCommented:
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.
 
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

asp_net2Author Commented:
still no dice!
0
David RobitailleAnalyst ProgrammerCommented:
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.

 
0
asp_net2Author Commented:
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.
0
David RobitailleAnalyst ProgrammerCommented:

set AutoPostBack="True" in the DropDownList

you could then use the SelectedIndexChanged  event of the dropdown list.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asp_net2Author Commented:
Thank you davrob60!! That worked as i needed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.