Brian
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.Conne ctionStrin gs("Printe r").Connec tionString
Dim conn As SqlConnection = New SqlConnection(connectionSt ring)
Dim pntr_bldgid As Int32 = Request.QueryString("@pntr _bldgid")
Dim gridviewComm As SqlCommand
gridviewComm = New SqlCommand("GridViewTest", conn)
gridviewComm.CommandType = CommandType.StoredProcedur e
' Create Parameter for Querystring
gridviewComm.Parameters.Ad d("@pntr_b ldgid", SqlDbType.Int)
gridviewComm.Parameters("@ pntr_bldgi d").Value = DropDownList1.SelectedValu e
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.Conne ctionStrin gs("Printe r").Connec tionString
Dim conn As SqlConnection = New SqlConnection(connectionSt ring)
Dim printerComm As SqlCommand
printerComm = New SqlCommand("PrinterTest", conn)
printerComm.CommandType = CommandType.StoredProcedur e
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.DataValueFie ld = "pntr_bldgid"
DropDownList1.DataTextFiel d = "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
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"
<!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.Conne
Dim conn As SqlConnection = New SqlConnection(connectionSt
Dim pntr_bldgid As Int32 = Request.QueryString("@pntr
Dim gridviewComm As SqlCommand
gridviewComm = New SqlCommand("GridViewTest",
gridviewComm.CommandType = CommandType.StoredProcedur
' Create Parameter for Querystring
gridviewComm.Parameters.Ad
gridviewComm.Parameters("@
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.Conne
Dim conn As SqlConnection = New SqlConnection(connectionSt
Dim printerComm As SqlCommand
printerComm = New SqlCommand("PrinterTest", conn)
printerComm.CommandType = CommandType.StoredProcedur
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.DataValueFie
DropDownList1.DataTextFiel
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
What is your question exactly?
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.
ASKER
still no dice!
it it the command that dont work or your DropDownList1.SelectedValu e
test:
Button1.text = DropDownList1.SelectedValu e
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.
test:
Button1.text = DropDownList1.SelectedValu
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you davrob60!! That worked as i needed.