Results 1 to 5 of 5

Thread: Dataview problems [resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Resolved Dataview problems [resolved]

    Okay, I have two combo boxes on a form, both displaying a different column from the same datatable, I want both these combo boxes to be sorted alphabetically though, so I created a dataview as so
    VB Code:
    1. ds.Tables.Add("ContactsCom")
    2. ddiView = New DataView(ds.Tables("ContactsCom"))

    I then bind the combo boxes as follows

    VB Code:
    1. Me.cbocompany.DataSource = ds
    2.         Me.cbocompany.DisplayMember = "ContactsCom.CompanyName"
    3.         Me.cbocompany.ValueMember = "ContactsCom.ContactID"
    4.  
    5.         ddiView.Sort = "MID"
    6.  
    7.         cboddi.DataSource = ddiView
    8.         Me.cboddi.DisplayMember = "MID"
    9.         Me.cbocompany.ValueMember = "ContactID"

    Which all seems to work fine, but my problem now is that on the
    cbocompany combo box this works
    VB Code:
    1. Private Sub cbocompany_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbocompany.SelectedIndexChanged
    2.  
    3.         Try
    4.  
    5.             MsgBox(cbocompany.SelectedValue)
    6.         Catch ex As Exception
    7.             MsgBox(ex.ToString)
    8.         End Try
    9.  
    10.     End Sub
    But on the cboddi box I get the exception

    VB Code:
    1. Private Sub cboddi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboddi.SelectedIndexChanged
    2.        
    3.         Try
    4.             MsgBox(cboddi.SelectedValue)
    5.        
    6.         Catch ex As Exception
    7.             Me.TextBox1.Text = ex.ToString
    8.         End Try
    9.  
    10.     End Sub

    System.ArgumentException: Argument 'Prompt' cannot be converted to type 'String'.

    Why?
    Last edited by Oliver1; Feb 10th, 2006 at 06:51 AM. Reason: resolved

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Dataview problems

    First thing that I would like to say is that you should not be using MsgBox function in VB.NET. There is a much better method MessageBox.Show that you should always try to use whenever you need to display a messagebox. MsgBox function is there just for backward compatibility with VB 6.0.

    Another thing is that you haven't shown which line is erroring out. Is it the Msgbox function or the catch block that giving you problems.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Dataview problems

    This statement gives me a value
    cbocompany.SelectedValue
    but this doesn't
    cboddi.SelectedValue
    and errors out saying
    System.ArgumentException: Argument 'Prompt' cannot be converted to type 'String'.

    The msgbox is only there while I'm trying to find the problem, and in fact I want to use the selectedvalue in another function.
    I am aware that you shouldn't really use the msgbox function, but then I'm also aware that drinking is bad for you, but that doesn't stop me doing it

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Dataview problems

    This is a kind of weired problem. I would suggest that before you bind the comboboxes just remove the handlers. And after you are done with you binding stuff add the handlers back. Something similar to this
    VB Code:
    1. RemoveHandler cboddi.SelectedIndexChanged, AddressOf cboddi_SelectedIndexChanged
    2. RemoveHandler cbocompany.SelectedIndexChanged, AddressOf cbocompany_SelectedIndexChanged
    3.  
    4.         ds.Tables.Add("ContactsCom")
    5. ddiView = New DataView(ds.Tables("ContactsCom"))
    6. Me.cbocompany.DataSource = ds
    7.         Me.cbocompany.DisplayMember = "ContactsCom.CompanyName"
    8.         Me.cbocompany.ValueMember = "ContactsCom.ContactID"
    9.  
    10.         ddiView.Sort = "MID"
    11.  
    12.         cboddi.DataSource = ddiView
    13.         Me.cboddi.DisplayMember = "MID"
    14.         Me.cbocompany.ValueMember = "ContactID"
    15.         AddHandler cboddi.SelectedIndexChanged, AddressOf cboddi_SelectedIndexChanged
    16. AddHandler cbocompany.SelectedIndexChanged, AddressOf cbocompany_SelectedIndexChanged
    And I have a question for you. When do you actually get this error. Is it when you try to select something in the Combobox or is it before that?

    Quote Originally Posted by Oliver1
    The msgbox is only there while I'm trying to find the problem, and in fact I want to use the selectedvalue in another function.
    I am aware that you shouldn't really use the msgbox function, but then I'm also aware that drinking is bad for you, but that doesn't stop me doing it
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Dataview problems

    Thanks for your help, turned out to be a really stupid mistake
    Me.cbocompany.ValueMember = "ContactID"
    should have been
    Me.cboddi.ValueMember = "ContactID"

    Hence the reason why it didn't work, must have spent about 2 hours on this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width