Drop Down List Control Issue
Hey all,
I'm creating a web app to tracking computers and assigned technicians. I use a database with 2 tables (Server and Tech). Server has a primary key of server_id and Tech has a primary key of user_id. I've created the database with constrains that a Tech can have many Servers but a Server doesn't require a Tech.
Now, onto the problem:
I've gotten to the point of creating the edit page to this app. On the edit page I have a Form View pulling in data from a SQL database table using an ObjectDataSource. Everything populated fine when I pull up a server to view/edit. I decided to complicate things by adding a DDL to show the actual tech name (full_name) instead of the foreign key (user_id). I did this with a SQLDataSource and displaying the user name instead of the user_id field. This works great if the server has a tech associated, but when there is no Tech associated, I get the following error:
'user_idDDL' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'user_idDDL' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I figured I could get around this issue using some code within my DDL tag. This is what I've got so far but I can't get the syntax correct:
<asp:DropDownList ID="user_idDDL" DataSourceID="SqlDataSource1" runat="server" DataTextField="full_name" DataValueField="user_id" SelectedValue='<%# if (user_id !='') { Bind("user_id") } %>' AppendDataBoundItems="true"> <asp:ListItem Value="0"> Select a Tech </asp:ListItem> </asp:DropDownList>
Basically, I want to say:
If user_id !='' { BIND("user_id") } else { POPULATE LIST WITH ALL TECHS & SELECT 'Select a Tech' }
How would I format that within my DDL tag. Any help would be greatly appreciated.
Thanks,
Roger
--------------------------------------------------------------------------------
Thanks,
Roger A
|