We help IT Professionals succeed at work.
Get Started

Databound dropdownlist inside a repeater

shankwheat
shankwheat asked
on
773 Views
Last Modified: 2012-05-08
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
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE