freakyuno
asked on
Repopulate a recordset from a Dropdown
I've got a dropdownlist on my page that is loaded from a table with names and values. What I want to do, is when a user selects an item from the drop down, I want the recordset to repopulate and show results in a related table = to that of the dropdown. I'm pasting my code in as help in understanding the question.
From webform1.aspx.vb -----------------
ublic Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.SqlConnection1 = New System.Data.SqlClient.SqlC onnection
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlD ataAdapter
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlC ommand
Me.DataSet11 = New support.micro_gen.net.Data Set1
Me.SqlDataAdapter2 = New System.Data.SqlClient.SqlD ataAdapter
Me.SqlSelectCommand2 = New System.Data.SqlClient.SqlC ommand
Me.DataSet21 = New support.micro_gen.net.Data Set2
CType(Me.DataSet11, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.DataSet21, System.ComponentModel.ISup portInitia lize).Begi nInit()
'
'SqlConnection1
'
Me.SqlConnection1.Connecti onString = "workstation id=""*****"";packet size=4096;user id=*********;data source=""mgse" & _
"rver.micro-gen.net\websql "";persist security info=True;initial catalog=MicroGenSu" & _
"pport;password=********** **"
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.SelectC ommand = Me.SqlSelectCommand1
Me.SqlDataAdapter1.TableMa ppings.Add Range(New System.Data.Common.DataTab leMapping( ) {New System.Data.Common.DataTab leMapping( "Table", "tbl_cemetary", New System.Data.Common.DataCol umnMapping () {New System.Data.Common.DataCol umnMapping ("PrimaryK ey", "PrimaryKey"), New System.Data.Common.DataCol umnMapping ("Name", "Name"), New System.Data.Common.DataCol umnMapping ("Address" , "Address"), New System.Data.Common.DataCol umnMapping ("Township ", "Township"), New System.Data.Common.DataCol umnMapping ("County", "County"), New System.Data.Common.DataCol umnMapping ("State", "State"), New System.Data.Common.DataCol umnMapping ("ZipCode" , "ZipCode"), New System.Data.Common.DataCol umnMapping ("GPSNorth ", "GPSNorth"), New System.Data.Common.DataCol umnMapping ("GPSWest" , "GPSWest"), New System.Data.Common.DataCol umnMapping ("GPSEleva tion", "GPSElevation"), New System.Data.Common.DataCol umnMapping ("CemURL", "CemURL")})})
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.Comma ndText = "SELECT PrimaryKey, Name, Address, Township, County, State, ZipCode, GPSNorth, GPS" & _
"West, GPSElevation, CemURL FROM tbl_cemetary ORDER BY Name"
Me.SqlSelectCommand1.Conne ction = Me.SqlConnection1
'
'DataSet11
'
Me.DataSet11.DataSetName = "DataSet1"
Me.DataSet11.Locale = New System.Globalization.Cultu reInfo("en -US")
'
'SqlDataAdapter2
'
Me.SqlDataAdapter2.SelectC ommand = Me.SqlSelectCommand2
Me.SqlDataAdapter2.TableMa ppings.Add Range(New System.Data.Common.DataTab leMapping( ) {New System.Data.Common.DataTab leMapping( "Table", "tbl_cemdata", New System.Data.Common.DataCol umnMapping () {New System.Data.Common.DataCol umnMapping ("LastName ", "LastName"), New System.Data.Common.DataCol umnMapping ("FirstNam e", "FirstName"), New System.Data.Common.DataCol umnMapping ("DoB", "DoB"), New System.Data.Common.DataCol umnMapping ("DoD", "DoD"), New System.Data.Common.DataCol umnMapping ("Comment" , "Comment"), New System.Data.Common.DataCol umnMapping ("PrimaryK ey", "PrimaryKey")})})
'
'SqlSelectCommand2
'
'This is where my Recordset should select the value of the dropdown list.
Me.SqlSelectCommand2.Comma ndText = "SELECT LastName, FirstName, DoB, DoD, Comment, PrimaryKey FROM tbl_cemdata WHERE " & _
"(PrimaryKey = 7)"
Me.SqlSelectCommand2.Conne ction = Me.SqlConnection1
'
'DataSet21
'
Me.DataSet21.DataSetName = "DataSet2"
Me.DataSet21.Locale = New System.Globalization.Cultu reInfo("en -US")
CType(Me.DataSet11, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.DataSet21, System.ComponentModel.ISup portInitia lize).EndI nit()
End Sub
Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlC onnection
Protected WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlD ataAdapter
Protected WithEvents DataSet11 As support.micro_gen.net.Data Set1
Protected WithEvents DropDownList1 As System.Web.UI.WebControls. DropDownLi st
Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlC ommand
Protected WithEvents SqlDataAdapter2 As System.Data.SqlClient.SqlD ataAdapter
Protected WithEvents SqlSelectCommand2 As System.Data.SqlClient.SqlC ommand
Protected WithEvents DataSet21 As support.micro_gen.net.Data Set2
Protected WithEvents DataGrid1 As System.Web.UI.WebControls. DataGrid
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
SqlDataAdapter1.Fill(DataS et11)
DropDownList1.DataBind()
End If
SqlDataAdapter2.Fill(DataS et21)
DataGrid1.DataBind()
End Sub
End Class
-------------------------- ---------- --------
From webform1.aspx -------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx. vb" Inherits="support.micro_ge n.net.WebF orm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScri pt" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id=DropDownList1 style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server" Width="192px" DataSource="<%# DataSet11 %>" DataValueField="PrimaryKey " DataTextField="Name" AutoPostBack="True">
</asp:DropDownList>
<asp:DataGrid id=DataGrid1 style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 64px" runat="server" Width="832px" DataSource="<%# DataSet21 %>" BorderColor="#CCCCCC" BorderStyle="None" BackColor="White" CellPadding="3" BorderWidth="1px" DataKeyField="PrimaryKey" AllowSorting="True" Font-Size="Small" Height="96px">
<FooterStyle ForeColor="#000066" BackColor="White"></Footer Style>
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></Sele ctedItemSt yle>
<ItemStyle Font-Size="X-Small" ForeColor="#000066"></Item Style>
<HeaderStyle Font-Size="Smaller" Font-Bold="True" ForeColor="White" BackColor="#006699"></Head erStyle>
<Columns>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="DoB" HeaderText="Date of Birth"></asp:BoundColumn>
<asp:BoundColumn DataField="DoD" HeaderText="Date of Death"></asp:BoundColumn>
<asp:BoundColumn DataField="Comment" HeaderText="Comments"></as p:BoundCol umn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></Page rStyle>
</asp:DataGrid>
</form>
</body>
</HTML>
-------------------------- ----------
I'm using Visual studio.net 7.1
From webform1.aspx.vb -----------------
ublic Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
Me.SqlConnection1 = New System.Data.SqlClient.SqlC
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlD
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlC
Me.DataSet11 = New support.micro_gen.net.Data
Me.SqlDataAdapter2 = New System.Data.SqlClient.SqlD
Me.SqlSelectCommand2 = New System.Data.SqlClient.SqlC
Me.DataSet21 = New support.micro_gen.net.Data
CType(Me.DataSet11, System.ComponentModel.ISup
CType(Me.DataSet21, System.ComponentModel.ISup
'
'SqlConnection1
'
Me.SqlConnection1.Connecti
"rver.micro-gen.net\websql
"pport;password=**********
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.SelectC
Me.SqlDataAdapter1.TableMa
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.Comma
"West, GPSElevation, CemURL FROM tbl_cemetary ORDER BY Name"
Me.SqlSelectCommand1.Conne
'
'DataSet11
'
Me.DataSet11.DataSetName = "DataSet1"
Me.DataSet11.Locale = New System.Globalization.Cultu
'
'SqlDataAdapter2
'
Me.SqlDataAdapter2.SelectC
Me.SqlDataAdapter2.TableMa
'
'SqlSelectCommand2
'
'This is where my Recordset should select the value of the dropdown list.
Me.SqlSelectCommand2.Comma
"(PrimaryKey = 7)"
Me.SqlSelectCommand2.Conne
'
'DataSet21
'
Me.DataSet21.DataSetName = "DataSet2"
Me.DataSet21.Locale = New System.Globalization.Cultu
CType(Me.DataSet11, System.ComponentModel.ISup
CType(Me.DataSet21, System.ComponentModel.ISup
End Sub
Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlC
Protected WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlD
Protected WithEvents DataSet11 As support.micro_gen.net.Data
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.
Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlC
Protected WithEvents SqlDataAdapter2 As System.Data.SqlClient.SqlD
Protected WithEvents SqlSelectCommand2 As System.Data.SqlClient.SqlC
Protected WithEvents DataSet21 As support.micro_gen.net.Data
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
SqlDataAdapter1.Fill(DataS
DropDownList1.DataBind()
End If
SqlDataAdapter2.Fill(DataS
DataGrid1.DataBind()
End Sub
End Class
--------------------------
From webform1.aspx -------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScri
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout
<form id="Form1" method="post" runat="server">
<asp:DropDownList id=DropDownList1 style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server" Width="192px" DataSource="<%# DataSet11 %>" DataValueField="PrimaryKey
</asp:DropDownList>
<asp:DataGrid id=DataGrid1 style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 64px" runat="server" Width="832px" DataSource="<%# DataSet21 %>" BorderColor="#CCCCCC" BorderStyle="None" BackColor="White" CellPadding="3" BorderWidth="1px" DataKeyField="PrimaryKey" AllowSorting="True" Font-Size="Small" Height="96px">
<FooterStyle ForeColor="#000066" BackColor="White"></Footer
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></Sele
<ItemStyle Font-Size="X-Small" ForeColor="#000066"></Item
<HeaderStyle Font-Size="Smaller" Font-Bold="True" ForeColor="White" BackColor="#006699"></Head
<Columns>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="DoB" HeaderText="Date of Birth"></asp:BoundColumn>
<asp:BoundColumn DataField="DoD" HeaderText="Date of Death"></asp:BoundColumn>
<asp:BoundColumn DataField="Comment" HeaderText="Comments"></as
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></Page
</asp:DataGrid>
</form>
</body>
</HTML>
--------------------------
I'm using Visual studio.net 7.1
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
sorry, change it to:
Private Sub DropDownList1_SelectedInde xChanged(B yVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedInde xChanged
BindFilteredData( DropDownList1.SelectedItem .Value)
End Sub
Private Sub DropDownList1_SelectedInde
BindFilteredData( DropDownList1.SelectedItem
End Sub
ASKER
Got it Working. Thanks. A+
ASKER
I'm a little confused though. You have to Page_Load subs.
Also, when I try to include the
Private Sub DropDownList1_SelectedInde
BindFilteredData( DropDownList1.SelectedItem
End Sub
It tells me it's not valid outside a method. I think I get the gist of how this is going to work, but could you follow up for me please?