Results 1 to 9 of 9

Thread: Count clicks in listbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84

    Count clicks in listbox

    I'm trying to detect if a user has clicked an item in a listbox. The reason for this is that if a user has clicked the listbox, then the datasettable that fills the second listbox must be cleared and filled up by another dataadapter.

    I've written some code but is doesnt do the job:

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    If blnLoadListbox1 = True Then

    Dim strSQLGroup As String

    If Me.ListBox1.SelectedIndex > -1 Then
    Me.dataset.Tables("tblGroup").Clear()
    End If

    Try
    strSQLGroup = "SELECT GroupDescription FROM tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue.ToString
    Dim DAGroup As OleDbDataAdapter = New OleDbDataAdapter(strSQLGroup, connection)
    DAGroup.Fill(dataset, "tblGroup")
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try

    Me.ListBox2.DataSource = dataset.Tables("tblGroup")
    Me.ListBox2.DisplayMember = "GroupDescription"

    End If

    End Sub


    The code crashes and I get an error message at the line : Me.dataset.Tables("tblGroup").Clear(). The message is the following:

    An unhandled exception of type 'System.NullReferenceException' occurred in TypedDatasets.exe

    Additional information: Object reference not set to an instance of an object.


    Any ideas???

    Thanks

    Tom
    Last edited by zozzie; Nov 21st, 2003 at 07:21 AM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You haven't commented your code well enough for someone like me to be able to tell alot about what's going on.

    I'm seeing me.dataset, which obviously isn't a standard property.

    The error sounds like the dataset was never instantiated, only referenced. Therefore, when you are calling Clear(), your code fails because its trying to call a method from an object that doesn't exist yet.

    Is your me.Listbox set to the same datatable you are trying to clear? If so, it may fire a second SelectedIndexChanged event. And that will cause you headaches..

    post more details plz..

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    OK, sorry, I'll comment the code:


    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    If blnLoadListbox1 = True Then ' Here I check if listbox1 is filled

    Dim strSQLGroup As String

    [COLOR=red]
    'In the following code I check if an item is clicked in listbox1, if so then selected index of listbox1 will be different from -1 and then has the table tblGroup that fills listbox2 and is a table of the dataset to be cleared so it's ready to be filled by a new dataadapter.
    /[COLOR=red]

    If Me.ListBox1.SelectedIndex > -1 Then
    Me.dataset.Tables("tblGroup").Clear()
    End If

    [COLOR=red]
    Here I create the sqlstring for the dataadapter that has to fill the datatable tblGroup that is a part of the dataset
    [COLOR=red]

    Try
    strSQLGroup = "SELECT GroupDescription FROM tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue.ToString
    Dim DAGroup As OleDbDataAdapter = New OleDbDataAdapter(strSQLGroup, connection)
    DAGroup.Fill(dataset, "tblGroup")
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try

    [COLOR=red]
    Here I fill the listbox with the contents of dataset.tblGroup
    [COLOR=red]

    Me.ListBox2.DataSource = dataset.Tables("tblGroup")
    Me.ListBox2.DisplayMember = "GroupDescription"

    End If

    End Sub


    Hope I was clear???

    Tom

    Both listboxes make use of the same dataset but diferent tables in this dataset. Listbox2 uses tblGroup. If listbox1 is clicked then the data in listbox2 must be updated because there exists a relationship between the two tables in the access database.

  4. #4
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    this will look better

    VB Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.  
    3. If blnLoadListbox1 = True Then ' Here I check if listbox1 is filled
    4.  
    5. Dim strSQLGroup As String
    6.  
    7. 'In the following code I check if an item is clicked in listbox1, if so then selected index of listbox1 will be different from -1 and then has the table tblGroup that fills listbox2 and is a table of the dataset to be cleared so it's ready to be filled by a new dataadapter.
    8.  
    9. If Me.ListBox1.SelectedIndex > -1 Then
    10. Me.dataset.Tables("tblGroup").Clear()
    11. End If
    12.  
    13. 'Here I create the sqlstring for the dataadapter that has to fill the datatable tblGroup that is a part of the dataset
    14.  
    15. Try
    16. strSQLGroup = "SELECT GroupDescription FROM tblGroup where tblGroup.MainGroupID=" & Me.ListBox1.SelectedValue.ToString
    17. Dim DAGroup As OleDbDataAdapter = New OleDbDataAdapter(strSQLGroup, connection)
    18. DAGroup.Fill(dataset, "tblGroup")
    19. Catch ex As Exception
    20. MsgBox(ex.ToString)
    21. End Try
    22.  
    23. 'Here I fill the listbox with the contents of dataset.tblGroup
    24.  
    25. Me.ListBox2.DataSource = dataset.Tables("tblGroup")
    26. Me.ListBox2.DisplayMember = "GroupDescription"
    27.  
    28. End If
    29.  
    30. End Sub

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Two things:

    You have an uninstantiated object somewhere. Either your TblGroup table, or its parent dataset.

    That's my first guess, because the error seems to suggest it.


    Second possibility:
    Because you said:
    "If listbox1 is clicked then the data in listbox2 must be updated because there exists a relationship between the two tables in the access database."


    If these relations are reflected in the dataset, and tblgroup has a one-to-many with the other (or any other table) in that dataset, clearing the table would cause some rows in child tables from this table to be left without a linking key, the clear method would fail. I would think this to be a whole seperate error.

    Bottom line:
    Set a breakpoint at that line. Run the program, when it hits the breakpoint, Open the command window, and type:
    VB Code:
    1. ? Me.dataset.Tables("tblGroup").Cols.Count
    to see if the table is actually structured. You should get a count > 0.

    Using the command window, you should be able to get a better understanding of what is happening.

    Also, I don't understand why you would want to clear tblGroup. Doesn't this table hold related records to the other table? You may want to create a temporary table that will either .Clone or .Copy the table, and fill new records in there. But then again, I'm not clear on your design goals.
    Last edited by nemaroller; Nov 22nd, 2003 at 08:27 AM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    Thanks guys,

    @nemaroller

    I don't think dataset is an unistantiated object because I defined it as a datamember in the class where the sub makes part of.

    there is a relation between tblGroup and another table in the access database, but not in the dataset.

    I'll try your suggestion and keep you informed.

    Thanks,

    Tom

  7. #7
    Lively Member
    Join Date
    Aug 2003
    Location
    When?!?!
    Posts
    108
    you are all AHoles. that error is simple. unless DataSet is a windows control, declare it as a new object if you have to.

    Is your dataset showing up on the form?
    If not, then the forms designer did not bla bla bla.
    Just create a new instance of your dataset.
    DannyJoumaa
    Advanced VB6 Programmer
    Intermediate-Advanced VB .NET Programmer
    Intermediate C# Programmer
    Intermediate Win32 Developer
    Beginner Mac OS X Developer
    Contact: [email protected]

    Favorite Sayings:
    "Every time you open your mouth, you prove your an idiot."
    "God is a programmer. Satan is a bug. Life is debugging."

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    @nemaroller

    The reason why I want to clear the table is because the dataset doesn't contain the relationship between the two tables.

    So, you would create a dataset with an explicit relation between the tables like this:

    DataSet1.Relations.Add(relation)

    Then I can use the relation between the tables to fill the listboxes so I wouldn't need a separate dataadapter to fill the listboxes.

    Is this a better way??

    Tom

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by Danny J
    you are all AHoles. that error is simple. unless DataSet is a windows control, declare it as a new object if you have to.

    Is your dataset showing up on the form?
    If not, then the forms designer did not bla bla bla.
    Just create a new instance of your dataset.
    Yes, we already covered that potty mouth.

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