Results 1 to 5 of 5

Thread: Casting problem

  1. #1

    Thread Starter
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31

    Casting problem

    I have a combobox that I populate with values from the database. And I have written a code in the SelectedIndexChanged property of the combobox to display the selected item in a label.
    VB Code:
    1. Private Sub cbxNationality_SelectedIndexChanged(ByVal sender As System.Object, _
    2.   ByVal e As System.EventArgs) Handles cbxNationality.SelectedIndexChanged
    3.        
    4.     Try
    5.         lblName.Text = cbxNationality.SelectedValue
    6.         'lblName.Text = CType(cbxNationality.SelectedValue, String)
    7.     Catch exp As Exception
    8.         MessageBox.Show(Err.Number & ": " & exp.Message, Me.Text)
    9.     End Try
    10.  
    11. End Sub
    However, I get this error: 13: Cast from type 'DataRowView' to type 'String' is not valid.

    The commented line too does not work.

    Any help will be appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    try

    lblName.Text = cbxNationality.SelectedValue.ToString

    I think the problem has to do with the datasource being a datarowview, because I don't get the error with a table or dataview as datasource for the combobox....

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    The data may look like a string, but it isn't, it's a DataRowView. Without a means to convert a DataRowView into a string, the language doesn't know what to do with the line you wrote. This is just a type safety issue which you'll have to live with. In VB6, it was possible to convert just about anything into a string in some manner, but in .NET it isn't quite as simple.

  4. #4

    Thread Starter
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31
    So what do you suggest I should do? Just a hint go get me in the right direction...

  5. #5
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Try using the Row property of the DataRowView object to get the information from the dataset then.

    Datarowview.Row.Item("ColumnInDataBase")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width