asp.net dropdownlist selected value if the value does not exist
I have a control that holds a dropdownlist that holds variables (A, B, C, N/A). The variables are pulled from a class. When an item is submitted, in the database it will store the letter and an identifier. If the database entry with the identifier exists, this code runs fine, if the database does not hold an identifier for the entry (it does not exist yet) then I get an error.
Code:
<EditItemTemplate>
<asp:DropDownList runat="server"
ID="ddlQualityTypes"
DataSource='<%# CodeLists.QualityTypes() %>'
DataTextField="strCode"
DataValueField="strCode"
开发者_JAVA百科 SelectedValue='<%#Eval("strQualityCode")
== null ? "A" : Eval("strQualityCode") %>'
/>
</EditItemTemplate>
Any ideas on how to get around this?
EDIT: strQualityCode is the name of the variable that is databound to the object holding which letter corresponds to the item.
I would make the field nullable in the database, and make it an optional parameter in your stored procedure. If the value of the dropdown is null or empty, don't pass the parameter to the stored procedure.
精彩评论