Newbie: problems with switching datasets

S

steve

Hello,
I am a bit perplexed about a simple matter and was wondering if anybody
could help me.
I also apologize if I am out of topic for the vb group.

What I want to do is the following:

Be able to display specific records from a database (Access) which can be
chosen by various fields. I want to allow the user to choose from a couple
of fields (name, id, etc) which obviously have to be synchronized. I also
want to use the *same* combos and textfields after switching tables in the
database.


As a practice example i used the Northwind db and created two combos and one
textbox. On top there are two radio buttons that (should) allow the user to
switch datasets. Obviously, you select the dataset -> populate the boxes ->
the user can select various records through *either of the comboboxes* (name
or id) -> various textboxes (addres, phone etc) get populated .
At any time you click on a radio button and switch from e.g. customers to
products table, and do the same with similar fields.

Very straightforward but ... i managed to raise an exception!

After a LOT of experimenting, my latest version works except when trying to
switch between datasets.
- Neither of the radiobuttons is checked by default and no DisplayMembers or
DataSources are visually set.
- after starting the program whichever radio is selected everything works as
expected. Once you try to switch, you get an exception.

Below is the code for the CheckedChanged event of the radio buttons.

Incidentally, i use "beginning Visual Basic .NET Databases" by Wrox. Are
there any suggestions for online resources reading about these things? What
perplexes me is that they suggest to have one dataadapter per table.
Although it can handle more it's not a good idea. So, if in a form you need
data from 7 tables you have to have 7 adapters ?.. how about datasets? one
per dataadapter also?....

Thanx in advance for any help guidance and my apologies for writing so much.

- steve

------------------------------------------------------
Private Sub Choix()

Dim records As Integer


If rdbCustomer.Checked = True Then

DataSet11.Clear()

DataSet21.Clear()

DataAdapter1.Fill(DataSet11)

cmb1.DataSource = DataSet11

cmb1.DisplayMember = "Customers.CustomerID"

cmb2.DataSource = DataSet11

cmb2.DisplayMember = "Customers.name"

txtBox1.DataBindings.Add("text", DataSet11, "customers.city")

Else

DataSet11.Clear()

DataSet21.Clear()

DataAdapter2.Fill(DataSet21)

cmb1.DataSource = DataSet21

cmb1.DisplayMember = "Products.pid"

cmb2.DataSource = DataSet21

cmb2.DisplayMember = "Products.price"

txtBox1.DataBindings.Add("text", DataSet21, "Products.price")

End If

End Sub
 
G

Guest

August 9, 2004

It would be helpful if you had the error message and what line
of code caused the exception. You can obtain it placing the code
in a Try... Catch ex as exception... End Try block.
Just put all of your code in the Try section and add this line to the
Catch section: msgbox(ex.message). I think it might be caused
by your txtBox1.databinding.... statements. If the user clicks on
the radio buttons more than once, then the textbox would have its
databindings added to again and would then be bound to both
datasets. Try adjusting the textbox statements to read like this...

txtBox1.DataBindings.Clear
txtBox1.DataBindings.Add("text", DataSet11, "customers.city")

txtBox1.DataBindings.Clear
txtBox1.DataBindings.Add("text", DataSet21, "Products.price")

I hope that this solves it!


Joseph MCP
 
S

steve

Thanx Joseph! that did the trick!

Greatly appreciate it

( However I am not happy ... LOL, i don't understand the whole data source -
adapter - dataset business. if you read the last part you'll understand. Any
quick links to a good tutorial online ? Do u agree that it should be 1
adapter-dataset per table ?)

Joseph MCP said:
August 9, 2004

It would be helpful if you had the error message and what line
of code caused the exception. You can obtain it placing the code
in a Try... Catch ex as exception... End Try block.
Just put all of your code in the Try section and add this line to the
Catch section: msgbox(ex.message). I think it might be caused
by your txtBox1.databinding.... statements. If the user clicks on
the radio buttons more than once, then the textbox would have its
databindings added to again and would then be bound to both
datasets. Try adjusting the textbox statements to read like this...

txtBox1.DataBindings.Clear
txtBox1.DataBindings.Add("text", DataSet11, "customers.city")

txtBox1.DataBindings.Clear
txtBox1.DataBindings.Add("text", DataSet21, "Products.price")

I hope that this solves it!


Joseph MCP


steve said:
Hello,
I am a bit perplexed about a simple matter and was wondering if anybody
could help me.
I also apologize if I am out of topic for the vb group.

What I want to do is the following:

Be able to display specific records from a database (Access) which can be
chosen by various fields. I want to allow the user to choose from a couple
of fields (name, id, etc) which obviously have to be synchronized. I also
want to use the *same* combos and textfields after switching tables in the
database.


As a practice example i used the Northwind db and created two combos and one
textbox. On top there are two radio buttons that (should) allow the user to
switch datasets. Obviously, you select the dataset -> populate the boxes ->
the user can select various records through *either of the comboboxes* (name
or id) -> various textboxes (addres, phone etc) get populated .
At any time you click on a radio button and switch from e.g. customers to
products table, and do the same with similar fields.

Very straightforward but ... i managed to raise an exception!

After a LOT of experimenting, my latest version works except when trying to
switch between datasets.
- Neither of the radiobuttons is checked by default and no DisplayMembers or
DataSources are visually set.
- after starting the program whichever radio is selected everything works as
expected. Once you try to switch, you get an exception.

Below is the code for the CheckedChanged event of the radio buttons.

Incidentally, i use "beginning Visual Basic .NET Databases" by Wrox. Are
there any suggestions for online resources reading about these things? What
perplexes me is that they suggest to have one dataadapter per table.
Although it can handle more it's not a good idea. So, if in a form you need
data from 7 tables you have to have 7 adapters ?.. how about datasets? one
per dataadapter also?....

Thanx in advance for any help guidance and my apologies for writing so much.

- steve

------------------------------------------------------
Private Sub Choix()

Dim records As Integer


If rdbCustomer.Checked = True Then

DataSet11.Clear()

DataSet21.Clear()

DataAdapter1.Fill(DataSet11)

cmb1.DataSource = DataSet11

cmb1.DisplayMember = "Customers.CustomerID"

cmb2.DataSource = DataSet11

cmb2.DisplayMember = "Customers.name"

txtBox1.DataBindings.Add("text", DataSet11, "customers.city")

Else

DataSet11.Clear()

DataSet21.Clear()

DataAdapter2.Fill(DataSet21)

cmb1.DataSource = DataSet21

cmb1.DisplayMember = "Products.pid"

cmb2.DataSource = DataSet21

cmb2.DisplayMember = "Products.price"

txtBox1.DataBindings.Add("text", DataSet21, "Products.price")

End If

End Sub

--------------------------------------------------------------------------
--
 
S

steve

GREATELY APPRECIATED!

Have a great day too!
-Steve

Joseph MCP said:
August 9, 2004

The theory behind the 1 adapter per table is:
If you create the adapter at design time and it only has one table, then
the Update, Insert, and Delete commands are automatically generated.
Then you can use them from code without having to create them yourself.
I like this a whole lot!!! If you have more than one table, then the
adapter
cannot create them automatically, because each one has to be different.
So if you create one at design time and it says that it can not create
the
Update, Insert, and Delete statements, then this is most likely the
problem.
I have seen some of my statements to be this long!:


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

As you can see, auto generated statements are very useful and I
would use them whenever possible!!!! Have a great day!


Joseph MCP
 
M

Mustafa Korkmaz

Try using typed datasets, so that you don't have to keep binding your
controls to the dataset fields. I don't know if this is going to solve
your problem but I think it's going to clean up your code. You should
post more info on the error and the steps to replicate the problem.

mk
 
A

ATJaguarX

Yes... it would help to post your error.

One thing I noticed... make sure you CLEAR the databindings before adding
them.

Example:

txtBox1.DataBindings.Clear
txtBox1.DataBindings.Add("text", DataSet21, "Products.price")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top