|
-
Nov 21st, 2003, 06:08 AM
#1
Thread Starter
Lively Member
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.
-
Nov 21st, 2003, 04:20 PM
#2
I wonder how many charact
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..
-
Nov 21st, 2003, 06:24 PM
#3
Thread Starter
Lively Member
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.
-
Nov 22nd, 2003, 06:23 AM
#4
Fanatic Member
this will look better
VB 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
'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.
If Me.ListBox1.SelectedIndex > -1 Then
Me.dataset.Tables("tblGroup").Clear()
End If
'Here I create the sqlstring for the dataadapter that has to fill the datatable tblGroup that is a part of the dataset
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
'Here I fill the listbox with the contents of dataset.tblGroup
Me.ListBox2.DataSource = dataset.Tables("tblGroup")
Me.ListBox2.DisplayMember = "GroupDescription"
End If
End Sub
-
Nov 22nd, 2003, 08:10 AM
#5
I wonder how many charact
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:
? 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.
-
Nov 22nd, 2003, 02:30 PM
#6
Thread Starter
Lively Member
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
-
Nov 22nd, 2003, 03:01 PM
#7
Lively Member
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."
-
Nov 22nd, 2003, 03:26 PM
#8
Thread Starter
Lively Member
@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
-
Nov 22nd, 2003, 11:14 PM
#9
I wonder how many charact
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|