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?
 
David RobitailleConnect With a Mentor Analyst ProgrammerCommented:

set AutoPostBack="True" in the DropDownList

you could then use the SelectedIndexChanged  event of the dropdown list.
0
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
 
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
 
asp_net2Author Commented:
Thank you davrob60!! That worked as i needed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.