aspx_professional thread: Can't get selected item from DB into Dropdownlist.
I also created a dirty solution to this problem, but I kept mine out of
ASP.Net and passed it on to SQL Server.
All I did is create a small stored procedure that uses UNION. Here is the
procedure:
Create Proc GetSelectedClientList
@JobID int
As
Declare @ClientID int
Select @ClientID = Client From Jobs Where JobID = @JobID
SELECT *, (Select 2 as X) as ReOrder FROM Clients Where ClientID <>
@ClientID
UNION
SELECT *, (Select 1 as X) as ReOrder FROM Clients Where ClientID = @ClientID
Order By ReOrder, Client
@JobID is the Key of the selected record. I find the ClientID from my Jobs
table. I then select all of the records (from clients...which has my
dropdown info) for the dropdown list that do not match the selected client,
and then I select the one that does. While I am doing this, I create a dummy
field 'x' to sort them by.
Then all I have to do, is bind this data to the dropdownlist:
<asp:DropDownList id="Client" datasource="<%# GetOrderedClientList() %>"
DataTextField = "Client" DataValueField = "ClientID" runat="server"/>
DataView GetOrderedClientList()
{
return dsEdit.Tables[0].DefaultView;;
}
HTH,
Scott
-----Original Message-----
From: Lou Feicht [mailto:lfeicht@f...]
Sent: Friday, October 26, 2001 11:10 AM
To: ASPX_Professional
Subject: [aspx_professional] Re: Can't get selected item from DB into
Dropdownlist.
I encountered similar problems, and errors recently and came up with an
alternative solution part of which follows. Although I would prefer to
find a better method, I had originally tried to do what you were doing.
<ASP:DropDownList
id="ddlUserRole"
DataSource=<%# ldRoles %>
DataValueField ="Key"
DataTextField = "Value"
SelectedIndex=<%# alRolesKeys.IndexOf(Container.DataItem
("ids_service_role_ak")) %>
AutoPostBack="True"
OnSelectedIndexChanged="UpdateRole"
runat="server" />