Error message when binding a listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am attempting to display two list boxes. In the first list box is a listing of all entities. When a entity is selected and it has a subentity the second list box displays with a list of the subentities. The code works but I get an error at the following two steps: (Code is VB.Net on a form

'set the data source to the data set's table SubEntit
lstSubEntity.DataSource = dsData.Tables("SubEntity"

'display the columns subentity nam
lstSubEntity.DisplayMember = ("EntityName"

The error I receive is: Cast from type 'DataRowView' to type 'String' is not valid

Here is the entire code
Private Sub lstEntity_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEntity.SelectedIndexChange

'define a new data adapte
Dim daSubEntity As SqlClient.SqlDataAdapte

'using the new data adapter attempt to locate in the database table tblSubEntity a subentity for the selected entity
daSubEntity = New SqlClient.SqlDataAdapter("SELECT * FROM tblSubEntity WHERE Status = 1 AND CustomerID = " & lstEntity.SelectedValue & " ORDER BY EntityName", conn

'fill the data set's (dsData) table SubEntity with the data from the above select quer
daSubEntity.Fill(dsData, "SubEntity"

'set the data adapter to nothing, it is no longer needed after data set table has been fille
daSubEntity = Nothin

'determine if a subentity was found (no row in subentity). If no subentity was found then delete the table subentity and go to the function that will display the address based on the selected entity
If (dsData.Tables("SubEntity").Rows.Count < 1) The
'remove the subentity table since there are no records foun
dsData.Tables.Remove("SubEntity"
'hide the subentity selection screen (if visible
lblSubEntities.Visible = Fals
lstSubEntity.Visible = Fals
btnSortAlphaAscSE.Visible = Fals
btnSortAlphaDescSE.Visible = Fals
grpSubEntity.Visible = Fals
lblStep2.Visible = Fals
lblStep3.Visible = Fals
lblStep4.Visible = Fals
lblStep5.Visible = Fals
lblStep6.Visible = Fals
lblStep7.Visible = Fals
lblMailAddress.Visible = Fals
lblPhysicalAddress.Visible = Fals
lstAddress.Visible = Fals
cboDeptStrategy.Visible = Fals
cboDivisionStrategy.Visible = Fals
cboType.Visible = Fals
chkWolfYes.Visible = Fals
chkWolfNo.Visible = Fals
txtProjectDesc.Visible = Fals
txtProjectDesc.Text = "

'since there is no subentity show the addresses for the selected entity(go to function DisplayAddress, case Entit
DisplayAddress("Entity"
Exit Su
End I

'display the subentity selection scree
lblSubEntities.Visible = Tru
lstSubEntity.Visible = Tru
btnSortAlphaAscSE.Visible = Tru
btnSortAlphaDescSE.Visible = Tru
grpSubEntity.Visible = Tru

'set the data source to the data set's table SubEntit
lstSubEntity.DataSource = dsData.Tables("SubEntity") <== this is where I receive erro

'display the columns subentity nam
lstSubEntity.DisplayMember = ("EntityName") <== this is where I receive erro

'set the value member to the subentity id for later referenc
lstSubEntity.ValueMember = "SubEntityID

Catch eException As Exceptio
MsgBox("Error: " & eException.Message & " " & eException.Source
End Tr
End Su

Like I said, the code works even with the error messages. Any help in resolving this issue would be greatly appreciated.
Thanks, JR
 
Wel
Try thi
1stSubEntity.DataSource=dsData.Tables["SubEntity"]
Maybe this will hel
L

----- JR wrote: ----

I am attempting to display two list boxes. In the first list box is a listing of all entities. When a entity is selected and it has a subentity the second list box displays with a list of the subentities. The code works but I get an error at the following two steps: (Code is VB.Net on a form

'set the data source to the data set's table SubEntit
lstSubEntity.DataSource = dsData.Tables("SubEntity"

'display the columns subentity nam
lstSubEntity.DisplayMember = ("EntityName"

The error I receive is: Cast from type 'DataRowView' to type 'String' is not valid

Here is the entire code
Private Sub lstEntity_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEntity.SelectedIndexChange

'define a new data adapte
Dim daSubEntity As SqlClient.SqlDataAdapte

'using the new data adapter attempt to locate in the database table tblSubEntity a subentity for the selected entity
daSubEntity = New SqlClient.SqlDataAdapter("SELECT * FROM tblSubEntity WHERE Status = 1 AND CustomerID = " & lstEntity.SelectedValue & " ORDER BY EntityName", conn

'fill the data set's (dsData) table SubEntity with the data from the above select quer
daSubEntity.Fill(dsData, "SubEntity"

'set the data adapter to nothing, it is no longer needed after data set table has been fille
daSubEntity = Nothin

'determine if a subentity was found (no row in subentity). If no subentity was found then delete the table subentity and go to the function that will display the address based on the selected entity
If (dsData.Tables("SubEntity").Rows.Count < 1) The
'remove the subentity table since there are no records foun
dsData.Tables.Remove("SubEntity"
'hide the subentity selection screen (if visible
lblSubEntities.Visible = Fals
lstSubEntity.Visible = Fals
btnSortAlphaAscSE.Visible = Fals
btnSortAlphaDescSE.Visible = Fals
grpSubEntity.Visible = Fals
lblStep2.Visible = Fals
lblStep3.Visible = Fals
lblStep4.Visible = Fals
lblStep5.Visible = Fals
lblStep6.Visible = Fals
lblStep7.Visible = Fals
lblMailAddress.Visible = Fals
lblPhysicalAddress.Visible = Fals
lstAddress.Visible = Fals
cboDeptStrategy.Visible = Fals
cboDivisionStrategy.Visible = Fals
cboType.Visible = Fals
chkWolfYes.Visible = Fals
chkWolfNo.Visible = Fals
txtProjectDesc.Visible = Fals
txtProjectDesc.Text = "

'since there is no subentity show the addresses for the selected entity(go to function DisplayAddress, case Entit
DisplayAddress("Entity"
Exit Su
End I

'display the subentity selection scree
lblSubEntities.Visible = Tru
lstSubEntity.Visible = Tru
btnSortAlphaAscSE.Visible = Tru
btnSortAlphaDescSE.Visible = Tru
grpSubEntity.Visible = Tru

'set the data source to the data set's table SubEntit
lstSubEntity.DataSource = dsData.Tables("SubEntity") <== this is where I receive erro

'display the columns subentity nam
lstSubEntity.DisplayMember = ("EntityName") <== this is where I receive erro

'set the value member to the subentity id for later reference
lstSubEntity.ValueMember = "SubEntityID"

Catch eException As Exception
MsgBox("Error: " & eException.Message & " " & eException.Source)
End Try
End Sub

Like I said, the code works even with the error messages. Any help in resolving this issue would be greatly appreciated.
Thanks, JR
 
Back
Top