how to handle errors using try catch in a dropdownlist inside a detailsview

Ethemesep

New Member
I have a dropdownlist inside a detailsview where I will be editing records inside the details view. My problem is dropdownlist inside the details view is bind to a field and giving an error \[quote\] 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value\[/quote\]This is due to the selectedvalue value is not in the dropdownlist. Can someone tell me how to handle this error. I want to use try catch and do nothing and just ignore the error or maybe pass a blank value as selectedvalue in the dropdown.Main problem I have is I am not sure where to put the try catch statement.Complete code is below.\[code\] <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="RecordRef" DataSourceID="EntityDataSource1" Height="50px" Width="125px"> <Fields> <asp:BoundField DataField="RecordRef" HeaderText="RecordRef" ReadOnly="True" SortExpression="RecordRef" /> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" /> <asp:TemplateField HeaderText="JobCategory" SortExpression="JobCategory"> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="EntityDataSource2" DataTextField="ItemValue" DataValueField="ItemValue" SelectedValue='http://stackoverflow.com/questions/14546383/<%# Bind("JobCategory") %>'> </asp:DropDownList> <asp:EntityDataSource ID="EntityDataSource2" runat="server" ConnectionString="name=CRMSEntities" DefaultContainerName="CRMSEntities2" EnableFlattening="False" EntitySetName="KeyValues"> </asp:EntityDataSource> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("JobCategory") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("JobCategory") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="JobDescription" HeaderText="JobDescription" SortExpression="JobDescription" /> <asp:BoundField DataField="JobDate" HeaderText="JobDate" SortExpression="JobDate" /> <asp:BoundField DataField="JobStatus" HeaderText="JobStatus" SortExpression="JobStatus" /> <asp:BoundField DataField="Referral" HeaderText="Referral" SortExpression="Referral" /> <asp:CommandField ShowEditButton="True" ShowInsertButton="True" /> </Fields> </asp:DetailsView>\[/code\]
 
Back
Top