Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Use a dataset from a custome class

Hi Experts,
I have been working with VB 2008 for about 1 year now. I have the basics of filling a dataset on a form then binding it to a control and updating any changes to the dataset. I have a project that will have to do the same data access on many different forms. Should I just create a dataset then drop a table adapter on each form or should I create a class that I will call from each form  that needs it that will pass me the dataset and when I am done I can pass it back to the class that will handle any updates. If the answer is the class how would I pass the dataset back and forth.
Thank You,
John
0
DjJohnny
Asked:
DjJohnny
  • 3
  • 2
1 Solution
 
carlnorrbomCommented:
Hi,

Maybe it's not practical to pass it both ways as a dataset. Retrieving it from the class is somewhat trivial, i.e.

Public Class MyDataAccessClass

    Public Function GetMyData() As DataSet '(or alternatively a TableAdapter..)
        Dim MyDS As New DataSet()    
        'Logic to retrieve data from Your DB and populate the dataset
        Return MyDS
    End Function

    Public Function UpdateData(ByVal Param1 As String, ByVal Param2 As String, ...) As Boolean
        Try
        'Logic to pass the update to the database using parameters
        Return True
        Catch ex As SqlException
        'Logic to handle the exception
        Return False
    End Function

End Class

The in Your form code:

MyDataGridView.DataSource = MyDataAccessClass.GetMyData()

If MyDataAccessClass.UpdateData(ParamValue1, ParamValue2, ...) Then
    'Update was successful
Else
    'Update was not successful
End If

I hope You get the concept.

/Carl.
0
 
DjJohnnyAuthor Commented:
Carl,
Thank you. I see how you would get the data from the class as a data set, That should work fine.
"Maybe it's not practical to pass it both ways as a dataset" Why is this not practical?  Instead of sending the data back to the class

MyDataAccessClass.UpdateData(txtbox1.value, txtbox2.value, ...)  
would it be easer to send it as

MyDataAccessClass.UpdateData(Dataset1)

I will have as up to  20 controls on the forms that I will need to pass there data back to the class.If I bond the controls to the dataset and then just pass it to the class when I am done ,will that work?

John
0
 
carlnorrbomCommented:
Hi,

Well it should work as long as You construct the update method to take the dataset as a parameter and is able to extract the updated values in a correct manner.

/Carl.
0
 
DjJohnnyAuthor Commented:
How can I create the dataset in the class? Are there tools that I can drag and drop like on a from or do I have to code it all by hand.
John
0
 
carlnorrbomCommented:
Hi,

If you write up a custom class I guess You would need to do it by hand. Good practice is to separate presentation layer, business logic and data access. This way your logic and data access is more dynamic and highly re-usable cross different technologies. For instance, you could use your logic/data access for a winforms app as well as for webforms for instance.

/Carl.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now