P
Patrick Sullivan
Hi,
I get a "specified cast is not valid" when the
"cboContactName_SelectedIndexChanged" tries to put a new value into the
txtCompany textbox. I am using relations in this and have not used them much
before. I was getting an error before this one that complained about no
primary key, so I put that in. The error is in the last section. The rest is
so you can see the setup. TIA!
Private Sub ContactForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadData()
doDatabinding()
End Sub
Sub LoadData()
Dim conn As New SqlConnection(CONNECTION_STRING)
Dim da1 As New SqlDataAdapter("SELECT compID, compName FROM Companies ORDER
BY compName", conn)
Dim da2 As New SqlDataAdapter("SELECT * FROM Contacts ORDER BY contactName",
conn)
da1.TableMappings.Add("Table", "Companies")
da2.TableMappings.Add("Table", "Contacts")
ds.Clear()
da1.Fill(ds)
da2.Fill(ds)
Dim pColumn As DataColumn = ds.Tables("Companies").Columns("compID")
Dim cColumn As DataColumn = ds.Tables("Contacts").Columns("contactCompany")
ds.Relations.Add("FK_Contacts", pColumn, cColumn)
ds.Tables("Companies").PrimaryKey = New DataColumn()
{ds.Tables("Companies").Columns("compID")}
End Sub
Private Sub doDataBinding()
With cboCompanies
..DataSource = ds.Tables("Companies")
..ValueMember = "compID"
..DisplayMember = "compName"
..DropDownStyle = ComboBoxStyle.DropDownList
End With
With cboContactName
..DataSource = ds.Tables("Contacts")
..DisplayMember = "contactName"
..DropDownStyle = ComboBoxStyle.DropDown
End With
txtPhone.DataBindings.Add("Text", ds.Tables("Contacts"), "contactPhone")
txtEmail.DataBindings.Add("Text", ds.Tables("Contacts"), "contactEmail")
txtLMeeting.DataBindings.Add("Text", ds.Tables("Contacts"),
"contactLastMeeting")
txtCompany.DataBindings.Add("Text", ds.Tables("Companies"), "compName")
DisableControls(True)
DisableEdits(True)
End Sub
Private Sub cboContactName_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboContactName.SelectedIndexChanged
Dim SelectedContactID As Int32
SelectedContactID = cboContactName.SelectedValue
Dim SelectedContact As DataRow
SelectedContact = ds.Tables("Companies").Rows.Find(SelectedContactID)
*** error happens here *** txtCompany = SelectedContact.Item("compName")
End Sub
I get a "specified cast is not valid" when the
"cboContactName_SelectedIndexChanged" tries to put a new value into the
txtCompany textbox. I am using relations in this and have not used them much
before. I was getting an error before this one that complained about no
primary key, so I put that in. The error is in the last section. The rest is
so you can see the setup. TIA!
Private Sub ContactForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadData()
doDatabinding()
End Sub
Sub LoadData()
Dim conn As New SqlConnection(CONNECTION_STRING)
Dim da1 As New SqlDataAdapter("SELECT compID, compName FROM Companies ORDER
BY compName", conn)
Dim da2 As New SqlDataAdapter("SELECT * FROM Contacts ORDER BY contactName",
conn)
da1.TableMappings.Add("Table", "Companies")
da2.TableMappings.Add("Table", "Contacts")
ds.Clear()
da1.Fill(ds)
da2.Fill(ds)
Dim pColumn As DataColumn = ds.Tables("Companies").Columns("compID")
Dim cColumn As DataColumn = ds.Tables("Contacts").Columns("contactCompany")
ds.Relations.Add("FK_Contacts", pColumn, cColumn)
ds.Tables("Companies").PrimaryKey = New DataColumn()
{ds.Tables("Companies").Columns("compID")}
End Sub
Private Sub doDataBinding()
With cboCompanies
..DataSource = ds.Tables("Companies")
..ValueMember = "compID"
..DisplayMember = "compName"
..DropDownStyle = ComboBoxStyle.DropDownList
End With
With cboContactName
..DataSource = ds.Tables("Contacts")
..DisplayMember = "contactName"
..DropDownStyle = ComboBoxStyle.DropDown
End With
txtPhone.DataBindings.Add("Text", ds.Tables("Contacts"), "contactPhone")
txtEmail.DataBindings.Add("Text", ds.Tables("Contacts"), "contactEmail")
txtLMeeting.DataBindings.Add("Text", ds.Tables("Contacts"),
"contactLastMeeting")
txtCompany.DataBindings.Add("Text", ds.Tables("Companies"), "compName")
DisableControls(True)
DisableEdits(True)
End Sub
Private Sub cboContactName_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboContactName.SelectedIndexChanged
Dim SelectedContactID As Int32
SelectedContactID = cboContactName.SelectedValue
Dim SelectedContact As DataRow
SelectedContact = ds.Tables("Companies").Rows.Find(SelectedContactID)
*** error happens here *** txtCompany = SelectedContact.Item("compName")
End Sub