Conversion Not valid?

digita

Active member
Joined
Jul 8, 2004
Messages
29
Programming Experience
1-3
Hi,

I have some code, but it tells me that the conversion is not valid, and i don't know why, because i think that it should be okay.

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] frmProducts_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load
 
ListLoad()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ListLoad()
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oCmd [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDb.OleDbCommand
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oDR [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDb.OleDbDataReader
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oItem [/size][size=2][color=#0000ff]As[/color][/size][size=2] PDSAListItemNumeric
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strSQL [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strConn [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2]strSQL = "SELECT ID, Naam "
 
strSQL &= "FROM gegevens"
 
[/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2]oCmd = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbCommand
 
[/size][size=2][color=#0000ff]With[/color][/size][size=2] oCmd
 
.Connection = _
 
[/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\test.mdb")
 
.Connection.Open()
 
.CommandText = strSQL
 
oDR = .ExecuteReader()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2]lstProducts.Items.Clear()
 
[/size][size=2][color=#0000ff]Do[/color][/size][size=2][color=#0000ff]While[/color][/size][size=2] oDR.Read()
 
oItem = [/size][size=2][color=#0000ff]New[/color][/size][size=2] PDSAListItemNumeric
 
[/size][size=2][color=#0000ff]With[/color][/size][size=2] oDR
 
oItem.ID = [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](.Item("ID"))
 
oItem.Value = .Item("Naam").ToString()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2]lstProducts.Items.Add(oDR.Item("Naam"))
 
[/size][size=2][color=#0000ff]Loop
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] lstProducts.Items.Count > 0 [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]lstProducts.SetSelected(0, [/size][size=2][color=#0000ff]True[/color][/size][size=2])
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]Catch[/color][/size][size=2] oExcept [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
 
MessageBox.Show(oExcept.Message)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] lstProducts_SelectedIndexChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] lstProducts.SelectedIndexChanged
 
FormShow()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] FormShow()
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oCmd [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDb.OleDbCommand
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oDR [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDb.OleDbDataReader
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oItem [/size][size=2][color=#0000ff]As[/color][/size][size=2] PDSAListItemNumeric
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strSQL [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strConn [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
 
 
[/color][/size][size=2][color=#008000]' Get Primary Key From List Box
 
[/color][/size][size=2]oItem = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](lstProducts.SelectedItem, PDSAListItemNumeric)
 
 
 
strSQL = "SELECT ID, Naam, "
 
strSQL &= " Adres, Telefoon "
 
strSQL &= " WHERE ID = " & oItem.ID
 
[/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2]oCmd = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbCommand
 
[/size][size=2][color=#0000ff]With[/color][/size][size=2] oCmd
 
.Connection = _
 
[/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\test.mdb")
 
.Connection.Open()
 
.CommandText = strSQL
 
oDR = .ExecuteReader()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] oDR.Read() [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]With[/color][/size][size=2] oDR
 
txtID.Text = .Item("ID").ToString()
 
txtNaam.Text = _
 
.Item("Naam").ToString()
 
txtAdres.Text = _
 
.Item("Adres").ToString()
 
txtTelefoon.Text = _
 
.Item("Telefoon").ToString()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2]oDR.Close()
 
oCmd.Connection.Close()
 
[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] oException [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
 
MessageBox.Show(oException.Message)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]

It goes wrong at:
VB.NET:
[size=2][color=#008000]' Get Primary Key From List Box
 
[/color][/size][size=2]oItem = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](lstProducts.SelectedItem, PDSAListItemNumeric)[/size]

And this is PDSAListItemNumeric
VB.NET:
[size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2] PDSAListItemNumeric
 
[/size][size=2][color=#0000ff]Private[/color][/size][size=2] mstrValue [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] mintID [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2]()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] strValue [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2], _
 
[/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] intID [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
 
mstrValue = strValue
 
mintID = intID
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] Value() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Get
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] mstrValue
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Get
 
[/color][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2])
 
mstrValue = Value
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Set
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Property
 
[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] ID() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer
 
[/color][/size][size=2][color=#0000ff]Get
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] mintID
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Get
 
[/color][/size][size=2][color=#0000ff]Set[/color][/size][size=2]([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Value [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2])
 
mintID = Value
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Set
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Property
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Overrides[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] ToString() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] mstrValue
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function
 
End[/color][/size][size=2][color=#0000ff]Class
 
[/color][/size]

I don't know what's wrong, but does someone else see what's wrong?
 
There is no built in conversion for Object to PDSAListItemNumeric.

Change the offending line to this:

VB.NET:
oItem = New PDSAListItemNumeric(lstProducts.Text, lstProducts.SelectedValue)
 
Back
Top