|
-
Dec 9th, 2003, 06:10 AM
#1
Thread Starter
Lively Member
Casting problem with datarowview
Hi,
I'm having a hair-rising casting problem.
The problem is the following: in a form there is a listbox in which the user can make different selections. The listbox has the following code (indicated in the load subroutine of the form):
Me.ListBox.DataSource = Me.DSlistboxes
Me.ListBox.DisplayMember = "tblMainGroup.MaingroupDescription"
Me.ListBox.ValueMember = "tblMainGroup.MaingroupID"
Where MainGroupID is an autonumber in the underlying access 2000 database.
Based on the selection of the user I want to display a corresponding code (SubGroupAbbreviation) in a textbox. Therefore I wrote the following code:
Private Sub ListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox3.SelectedIndexChanged
Dim SQLWeergaveCode As String
(*) SQLWeergaveCode = "SELECT * FROM tblMainGroup WHERE MainGroupID= " & Me.ListBox.SelectedValue
Me.DAWeergaveCode = New OleDbDataAdapter(SQLWeergaveCode, DataConnection)
'DataConnection.Open()
DAWeergaveCode.Fill(DSWeergaveCode, "tblSubgroup")
'DataConnection.Close()
Me.TextBoxCode.DataBindings.Clear()
Me.TextBoxCode.DataBindings.Add("text", DSWeergaveCode, "tblMainGroup.MainGroupAbbreviation")
End Sub
When I compile this code I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'DataRowView' to type 'String' is not valid.
The error occurs at the line indicated with (*). I don't understand this error because I think that me.listbox.selectedvalue is an integer???
.
Or is there an easier way to accomplish what I want:
table with three fields:
MainGroupID (autonumber, pk)
MainGroupDescription
MainGroupAbbreviation
The MainGroupdescription must be shown in the listbox and when the user clicks on an item then the corresponding MainGroupAbbreviation must be shown in a textbox. When the user clicks another item, the text shown in the textbox must also change.
Thanks,
Tom
-
Dec 10th, 2003, 01:13 AM
#2
Lively Member
If you have Option.Strict On, just try putting .ToString on the end of Me.ListBox.SelectedValue
If that doesn't work you might have to post a bit more code so we can see what is going on.
How are you handling the autonumber? is it a number you are adding or are you letting VB.NET handle it itself? VB.NET should just KNOW about the autonumbering if it is just a straight forward numbering system. So you might need to look at that.
-
Dec 10th, 2003, 03:32 AM
#3
Thread Starter
Lively Member
Hi Wallabie,
The autonumbering issue is solved by VB.NET by adding the following code:
DSlistboxes.Tables("tblCD").Columns("CDID").AutoIncrement = True
DSlistboxes.Tables("tblCD").Columns("CDID").AutoIncrementSeed = -1
DSlistboxes.Tables("tblCD").Columns("CDID").AutoIncrementStep = -1
Concerning my databinding question: I found a solution that uses the databindingsclass:
Me.TextBoxCode.DataBindings.Add("text", DSlistboxes, "tblMainGroup.tblMainGrouptblGroup.tblGrouptblSubGroup.SubgroupAbbreviation")
It uses the relations in the dataset to choose the correct item to display in the dataset.
Thanks,
Tom
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|