sharing sqlDataSet throughout project?


Well-known member
Oct 21, 2004
Programming Experience
How would I make an SQLConnection, SQLDataAdapter, and DataSet available to multiple forms in a project?
My dataset is a Part table of the MackTool database in SQLServer2000
I have a datagrid attached to the dataset on form1(dataform Wizard generated). I need to access form2 which is a textbox layout of the record in focus from the form1 datagrid(dataform Wizard generated). How do I set the databindings of form2 to the dataset on form1? I also want to share the add, update, and delete functions of the dataset between the two forms so that updates made on either form are maintained and displayed on the other form. I need to have the Datagrid component of form1 automatically refresh to display the updates made on form2 when form 2 is closed.
Is there a way to have the dataform wizard generate the code for me and convert the form into a public business class that the dataset and dataset add, update, and delete functions can be called from both forms?

I have tried this functionality with a connection, dataadapter and dataset residing on form1 and the referenced dataset residing on form2.
I databind the text boxes of form2 to the referenced dataset on form2 and pass the record location from form1 to form2 when opened. Form 2 correctly displays the desired record but any updates made on form 2 are not passed back to form1. Form1 and form2 both have instances of the load and update functions generated by the dataform wizard

Public Function UpdateDataSet()
'Create a new dataset to hold the changes that have been made to the main
Dim objDataSetChanges As Mack.dsPart = New Mack.dsPart
Dim objDataSetUpdated As System.Data.DataSet = New Mack.dsPart
'Get the changes that have been made to the main dataset.
objDataSetChanges = CType(pds.GetChanges, Mack.dsPart)
'Check to see if any changes have been made.
If (Not (objDataSetChanges) Is Nothing) Then
'There are changes that need to be made, so attempt to update the datasource by
'calling the update method and passing the dataset and any parameters.
objDataSetUpdated = pds.Clone
Catch eUpdate As System.Exception
'Add your error handling code here.
Throw eUpdate
End Try
'Add your code to check the returned dataset for any errors that may have been
'pushed into the row object's error.
Catch eUpdateMerge As System.Exception
'Add exception handling code here
Throw eUpdateMerge
End Try
'Commit the changes that were just merged
'This moves any rows marked as updated, inserted or changed to being marked as original values
End If
End Function

What can I do to create a class or module to hold the dataset and dataset functions and call them from any form in the project. A centralized location for loading the dataset and updating the database is what I want.

There should be a sample database application that has a display form and an edit form for a sqlServer2000 table, where is it?

Am I stupid? Is this a common function when dealing with database tables? What am I not getting here, some underlying principle? Seems like a few lines of code should do it.


OK, I've finally figured it out. To create the business class, I used the data Form Wizard to create a dataForm and then copied the Connection, Adapter, DataSet, and all related functions to a new class (which I creatively called BusinessClass). In the attached project I use a module with a Main procedure that is the startup object. In the module I declare the BusinessClass, the GridForm (a form with a dataGrid) and the EditForm (a form with textBoxes for editing). Declaring them in the module makes them available to all the forms. In the GridForm (which is first form opened by the Sub Main), I initialize the business class.

The part I struggled with was to be able to sort and filter the dataGrid and still get the correct record number to sync the editForm when opened. To accomplish that, I included a dataView in the business class and that dataView is the DataSource used for both forms.


    32.6 KB · Views: 127

Latest posts