Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cast from type 'DataRowView' to type 'String' is not valid 1

Status
Not open for further replies.

dpdg

Programmer
May 23, 2005
148
US
In a ListBox that is databound, I want to get the SelectedValue when I click it.

lblMessage.Text = Me.lstSubCat.SelectedValue

But I get this error message:

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Cast from type 'DataRowView' to type 'String' is not valid.
 
If a listbox/combobox is data bound it will return DataRows instead of ListItems.

The easy way to get what you are looking for is to either grab the row item, or just specify the field:

Code:
dim dr as datarow = me.lstsubcat.selectedvalue
'-or-
lblMessage.Text = ctype(Me.lstSubCat.SelectedValue,datarow).item("FieldName").tostring

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I tried both of those but neither of them worked.

this didn't work:
Code:
lblMessage.Text = CType(Me.lstSubCat.SelectedValue, DataRow).Item("SubCatID")

I got:
An unhandled exception of type 'System.NullReferenceException' occurred in dpWinAdmin.exe

Additional information: Object reference not set to an instance of an object.

I also tried:
Code:
      Dim dr As DataRow = Me.lstSubCat.SelectedValue

      lblMessage.Text = dr.Item("SubCatID")

I got the same error.

What am I doing wrong?

Thanks for the help
 
I tried that too and still got:

Additional information: Specified cast is not valid.

The column does exist. Here's the code I use to bind the dataset to the ListBox:

Code:
  Public Function LoadSubCatList(ByVal lst As ListBox, ByVal iCatID As Int16) As String
      Dim cnn As SqlConnection, ds As New DataSet, i, scatid As Int32
      Dim Descr As String
      Dim sql As String = "Select SubCatID, Descr from ErrNetSubCat where CatID = " & iCatID & " Order By SubCatID"
      Try
         cnn = New SqlConnection(cs)
         cnn.Open()
         Dim cmd As New SqlCommand(sql, cnn)
         Dim adapt As SqlDataAdapter = New SqlDataAdapter(cmd)
         adapt.Fill(ds, "ErrNetSubCat")
      
         lst.DataSource = ds.Tables(0).DefaultView
         lst.DisplayMember = "Descr"
         lst.ValueMember = "SubCatID"

      Catch ex As SqlException
         Return ex.Message
      Finally
         cnn.Close()
         cnn.Dispose()
      End Try
   End Function
 
The listbox is sortable, so it returns a DataRowView object. To access the data itself, cast it as follows:
Code:
CType(lst.SelectedValue, DataRowView).Row.Item(0)
 
Oops, I meant to add .ToString() to the end of that....
 
Shelton,

I get 'Specified cast is invalid.'
 
Sorry,

But none of these solutions actually work. Is there something that I am doing wrong or what?

 
What is your data source? Is it a text file or a data table of some kind? IF it's a datatable, why not use a datagrid instead of a list box?

Data grids make it easy to click a cell and fill a text box witht the data. Use a DataAdapter and a dataset to get the data.

I tried using a listbox initially for one project that uses a SQL table to display info. Had nothing but problems. Deleted the list box and added a datagrid. Found some source code that helped and now the project works great!


IF your set on using a lsit box, try including .Add after the word item.
i.e.: lblMessage.Text = CType(Me.lstSubCat.SelectedValue, DataRow).Item("SubCatID")

SHOULD BE: lblMessage.Text = CType(Me.lstSubCat.SelectedValue, DataRow).Item.Add("SubCatID")
 
Nope. That doesn't work either. I haven't run into anyone who actually has a solution that actually works. I haven't even been able to find anything in MSDN either.

I'm using a DataSet.

But I do like your idea or using a datagrid instead!

But still, I would like to find out how to get the SelectedValue value from a comboBox.

That IS the $64,000 question.
 
My sincere apologies for causing you grief. My code should have been:
Code:
CType(lst.SelectedItem, DataRowView).Row.Item("SubCatID").ToString()
I must really start testing my code before I post...
 
Halelujiah Shelton! I'm giving you a star for that one! It worked perfect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top