Results 1 to 13 of 13

Thread: Combobox and dataset

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9

    Unhappy Combobox and dataset

    I am trying to populate a combobox from a dataset but I am getting the following error: System.NullReferenceException, object reference not set to an instance of an object.

    here's my code, can't see where I failed to instantiate.

    Dim dad = New OleDbDataAdapter
    Dim ds As DataSet
    Dim strConn, strSQL As String

    strConn = "Data Source=Data3;Integrated Security=SSPI"
    strSQL = "Select instNbr from MyTable"
    dad = New SqlClient.SqlDataAdapter(strSQL, strConn)
    ds = New DataSet
    dad.Fill(ds)
    cboInstNbr.DataSource = ds.Tables("MyTable")
    cboInstNbr.DisplayMember = ds.Tables("MyTable").Columns("instNbr").ToString

    the error occurs on the displaymember line of code.

    Help a newbie with VB.Net.
    Thanks!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this line as it's (don't change it)
    VB Code:
    1. cboInstNbr.DisplayMember = "TABLE_NAME"

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9

    Unhappy

    No error message but my combobox is not populated with the field. I am under the assumption that displaymember is the field name your populating the combobox with. So how come it won't populate?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    D'uh . It's my fault . I mean change it to this (where xxx is the name of your column):


    VB Code:
    1. cboInstNbr.DataSource = ds.Tables("MyTable").DefaultView
    2. cboInstNbr.DisplayMember = "xxxx"

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9

    Unhappy

    I changed the following code to this:

    cboInstNbr.DataSource = ds.Tables("MyTable")
    cboInstNbr.DisplayMember = "instnbr"

    When I added defaultview it gave me the aformentioned error on the datasource line instead this time. I tested the code to see if it would populate a data grid and it had no problem doing that. It just won't work on a combobox. Is there something else that needs to be done for a combobox to get it running?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    umm , have you tried "ValueMember" in place of "DisplayMember" ?

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9
    Yes. Do you know how to populate the combobox another way?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Raine
    Yes. Do you know how to populate the combobox another way?
    Yes , I'll create demo for you . I'll be back .

  9. #9
    Member
    Join Date
    Jan 2003
    Posts
    44
    Try changing this line
    cboInstNbr.DataSource = ds.Tables("MyTable")
    To
    cboInstNbr.DataSource = ds.Tables(0)

    Frank

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Here's the demo .
    Attached Files Attached Files

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I'm probably nearly as new as you but I don't think that Pirate's suggestions would deal with the error message you had. It does puzzle me that the error message disappeared at one stage. Without seeing your entire code I have to make a few guesses.

    I assume you have imported the OLEDB namespace. If not ,

    Dim dad = New OleDbDataAdapter

    should be

    Dim dad = New Oledb.OleDbDataAdapter


    Also, instead of

    dad.Fill(ds)

    try

    dad.Fill(ds, "myTable").

    Make sure you have opened your connection
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9

    Smile

    Thanks it works....my only question is what does TableStr represent? I left it out and it works fine.

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Raine
    Thanks it works....my only question is what does TableStr represent? I left it out and it works fine.
    It's the table's name that you want to get the data from !

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