Hi
I have a databound dropdownlist and use OnItemDataBound to pre-select IMF Types that are stored in my database. This
approach works fine when there are IMF Type records to show in the repeater but when there are no records I get the error message
"Conversion from type 'DBNull' to type 'String' is not valid"
Protected Sub rptIMFTypes_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
Dim ddlIMFTypes As DropDownList = e.Item.FindControl("ddlIMFTypes")
If Not e.Item.FindControl("ddlIMFTypes") Is Nothing Then
ddlIMFTypes.Items.FindByValue(CType(System.Web.UI.DataBinder.Eval(e.Item.DataItem, "IMF_ID"), Object)).Selected = True
End If
End Sub
<asp:Repeater id="rptIMFTypes" runat="server" OnItemDataBound="rptIMFTypes_ItemDataBound">
<HeaderTemplate>
<table cellpadding="3" cellspacing="1" border="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:DropDownList id="ddlIMFTypes" Runat="Server"
DataSourceID="dsIMFTypes"
DataTextField="IMF_Type"
DataValueField="IMF_ID"
CssClass="formLabel" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I also tried this but it didn't work
If IsDBNull(CType(System.Web.UI.DataBinder.Eval(e.Item.DataItem, "IMF_ID"), Object) = False) Then