'System.InvalidCastException'

  • Thread starter Thread starter Patrick Sullivan
  • Start date Start date
P

Patrick Sullivan

I get this error (VB.Net 2003/SQL Server 2000)

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

Additional information: Cast from string "compID" to type 'Integer' is not
valid.
-----

when this code executes at runtime "Row.appCompany =
CInt(cmbCompanies.ValueMember)"

I have tried using "val()" and "clng()" but those would not even compile.
Here is the setup:
---------
Dim con As New SqlConnection(ConnectionString)
appDataSet = New DataSet2
Dim adapter As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM
Applications", con)
adapter.Fill(appDataSet, "Applications")
'Insert a new row into the typed dataset and populate it with
values.

Dim Row As DataSet2.ApplicationsRow
Row = appDataSet.Applications.NewApplicationsRow()

Row.appCompany = CInt(cmbCompanies.ValueMember)
Row.appPosition = CInt(cmbPosition.ValueMember)
Row.appDate = CDate("")
Row.appMethod = CInt(cmbMethod.ValueMember)
appDataSet.Applications.AddApplicationsRow(Row)
grpBoxDisable(True)
 
Here's the combobox code, I forgot.

con.Open()

compsData = New DataSet

adapter.Fill(compsData)

With cmbCompanies

..DataSource = compsData.Tables(0)

..DisplayMember = "compName"

..ValueMember = "compID"

End With

con.Close()


--
 
Patrick,

A couple of things to try:

First set the DisplayMember, then the ValueMember and finally the DataSource.

Be sure that the column names for DisplayMember and ValueMember are spelled
exactly as they appear in the datatable, including case.

Kerry Moorman
 
Thanks, but it didn't help. The case is accurate, changing the order of
loading values did not affect the error.
 
I also tried this:

Row.appCompany = toint32(cmbCompanies.ValueMember)

error: input string was not in correct format

So it sounds like my combobox values are weird, but using debugger they look
like ordinary strings of ID numbers.
 
Well, DUH! You are supposed to use SELECTEDVALUE not "valuemember", lol. I
finally figured that out. Now my app does not save any data, using
appDataSet.Applications.AddApplicationsRow(Row) or
appDataSet.AcceptChanges()
 
Back
Top