Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
i have the following code thad i write on the dropdownlist databound event
the ID of dropdown list is"dupddatedist"




VB
Protected Sub dupddatedist_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddupdatedist.DataBound
      Dim ddl As DropDownList = DirectCast(sender, DropDownList)
      'add an empty item on top of the list
      AddEmptyItem(ddl)
      Dim frmV As FormView = DirectCast(ddl.NamingContainer, FormView)
      'If frmV.DataItem IsNot Nothing Then

      'Let's pull the province value from the databound item.  The data
      'in my application is supplied by BLL as a dataview.  Therefore each
      'item bound to the FormView is of type DataRowView.  So let's cast
      'that DataItem to the appropriate type to be able to use it
      Dim intDistrict As Int32 = DirectCast(frmV.DataItem, DataRowView)("intContactPerson1DistrictID")
      ddl.ClearSelection()
      'be careful of the possibility that the value saved on the
      'database does not exist in the valid selections that are displayed
      'on the list
      Dim li As ListItem = ddl.Items.FindByValue(intDistrict)
      If li IsNot Nothing Then
          li.Selected = True
      End If





but when i run the code i got following error



Conversion from type 'DBNull' to type 'Integer' is not valid.



please help me out what shoul i do
and also write the correct code for the problem


Thanks and regards
Subiya ahsan
Posted

1 solution

Most likely the probelm is on this line:
Dim intDistrict As Int32 = DirectCast(frmV.DataItem, DataRowView)("intContactPerson1DistrictID")

Before setting the value of the variable, check that it doesn't contain a null from database, which would be a DbNull.Value in .Net. For an example, see: http://msdn.microsoft.com/en-us/library/system.dbnull.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900