[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DropDownList problem

Posted on 2008-11-17
8
Medium Priority
?
314 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:asp_net2
  • 4
  • 4
8 Comments
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22978630
What is your question exactly?
 
0
 
LVL 4

Author Comment

by:asp_net2
ID: 22978715
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22978800
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:asp_net2
ID: 22978958
still no dice!
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22979005
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22979039
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
 
LVL 18

Accepted Solution

by:
David Robitaille earned 2000 total points
ID: 22979119

set AutoPostBack="True" in the DropDownList

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

Author Closing Comment

by:asp_net2
ID: 31517565
Thank you davrob60!! That worked as i needed.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question