Question Library Model - Help is needed

kfirba

Well-known member
Joined
Dec 29, 2012
Messages
77
Programming Experience
1-3
Hello!

I have read about the ADO.NET and learn some new things. In order to practice what i have learned, I want to create a library model with the following access tables:

Table Name:Costumers
Fields: CostumerID, CostumerName, CostumerLastName

Table Name:Books
Fields: BookID, BookName, BookBorrow(Holds the value of true/false)

Table Name:Borrows
Fields: BorrowID, CostumerID, BookID, BorrowDate, BorrowEnd(Holds the balue of true/false)

I want to do a basic model of a library where I can add a costumer, let him borrow a book, let him return a book, add a book, delete a book, and hold a borrow history.

The part im struggling with is, when I "attach" the database to the program, I don't know where to use the DataSet, on the main form? some global variable? and where i have the use each adapter?

What i tried to do is to put everything on the MAIN form and then access the dataset and adapters like this:
VB.NET:
Main.*ADAPTER_NAME*.*FUNCTION*
Main.*DATASET*.*FUNCTION*

but it doesn't work.

I have created a form called: CostumerRegistration and another form called BookBorrow

My main problem is, I don't know how to organize my forms with the Data objects.
Do I have to create to each form a DataSet? What do I have to do >.<

I'm really lost about that >.<!
I would like someone to help me understand how do I build it!

Thanks in advance!
 
Ideally you would have separate libraries for each logical tier of the application. As you're just starting out though, it's easier to just put everything in the forms. You DO NOT put everything in the main form and access it elsewhere though. You put everything where it's needed. If you need to use a particular table adapter in a particular form then you put it in that form. If you need to use that table adapter in multiple forms then you add one to each form. As for the DataSet, if two forms use independent data then they should each have their own DataSet. If two forms use the same data then the required data is passed from one form to the other. For instance, if you have one form that displays records from a table in a grid and you want to edit one record in a separate dialogue then the first form passes the corresponding DataRow to the second form.
 
Ideally you would have separate libraries for each logical tier of the application. As you're just starting out though, it's easier to just put everything in the forms. You DO NOT put everything in the main form and access it elsewhere though. You put everything where it's needed. If you need to use a particular table adapter in a particular form then you put it in that form. If you need to use that table adapter in multiple forms then you add one to each form. As for the DataSet, if two forms use independent data then they should each have their own DataSet. If two forms use the same data then the required data is passed from one form to the other. For instance, if you have one form that displays records from a table in a grid and you want to edit one record in a separate dialogue then the first form passes the corresponding DataRow to the second form.

Thanks a lot!
just one little question, if I Create a DS object in Form1 named DataSet1 and I make another DS in Form2 named DataSet2, they both hold the same information? They both point in the same DataSet? Basically what I'm asking is, is DataSet1 = DataSet2?
whenever I make changes in DataSet1, I can access those changes in DataSet2? For instance, in DataSet1 I create a new record, can I access it on DataSet2?
 
No. If they are different objects then they are different objects. Think about how objects work in real life. If you had one car object and then another car object are they both the same car? Does turning the steering wheel in one make the other one turn? Of course not, so why would it in object-oriented programming, where programming objects are modelled on real-world objects?

You can have two variables that both refer to the same object and make changes through one variable and see them through the other variable, but that's quite different. Again think of how the real world works. I have a father and my sister also has a father. If I give my father a blue shirt and ask him to put it on, then ask my sister what colour her father's shirt is, what will she say? Blue, right? I have a 'father' variable and my sister has a 'father' variable but they both refer to the same one and only person.

Programming is exactly the same. So, the question is do you want two different forms to see the exact same data? If not then you simply create a DataSet in each one and, just as the forms are different objects, so the DataSets are different objects. If you do want one set of data then you only create one DataSet. In that case the first form would create the DataSet object and then pass it to the second form, either by setting a property or by calling a method and passing it as an argument.
 
No. If they are different objects then they are different objects. Think about how objects work in real life. If you had one car object and then another car object are they both the same car? Does turning the steering wheel in one make the other one turn? Of course not, so why would it in object-oriented programming, where programming objects are modelled on real-world objects?

You can have two variables that both refer to the same object and make changes through one variable and see them through the other variable, but that's quite different. Again think of how the real world works. I have a father and my sister also has a father. If I give my father a blue shirt and ask him to put it on, then ask my sister what colour her father's shirt is, what will she say? Blue, right? I have a 'father' variable and my sister has a 'father' variable but they both refer to the same one and only person.

Programming is exactly the same. So, the question is do you want two different forms to see the exact same data? If not then you simply create a DataSet in each one and, just as the forms are different objects, so the DataSets are different objects. If you do want one set of data then you only create one DataSet. In that case the first form would create the DataSet object and then pass it to the second form, either by setting a property or by calling a method and passing it as an argument.

oh, I see.

Why would i want 2 different DataSets in my program?
and according to what you said, if i create LibraryDataSet and then LibraryDataSet1, they refer to the same DS object? or they are completely different?
 
When you run the Data Source wizard and it generates a DataSet, that is a class, just like if you did this:
Public Class Thing
    '...
End Class
To use that class in code you have to create an instance of that class, i.e. an object. More often that not that object will be assigned to a variable of that type:
Dim myThing As Thing = New Thing
or, in short:
Dim myThing As New Thing
So, you instantiate the class to create an object and assign that to a variable.

In your project you have a DataSet class. You only have one DataSet class because you only have one database. The only time you would have more than one DataSet class is if you had more than one data source. When you drag it onto a form in the designer, the IDE adds code that declares a variable, creates an instance of the class and assigns that object to the variable. If you do that with two different forms then you have two different variables that refer to two different objects. If the forms act on different data then that is completely appropriate.

If the two forms act on the same data then only one of them should create a DataSet object. Rather than the other form creating a second DataSet object, the first form passes the object it created to the second form. You will then have two different variables in the two different forms but they will both refer to the same object.
 
When you run the Data Source wizard and it generates a DataSet, that is a class, just like if you did this:
Public Class Thing
    '...
End Class
To use that class in code you have to create an instance of that class, i.e. an object. More often that not that object will be assigned to a variable of that type:
Dim myThing As Thing = New Thing
or, in short:
Dim myThing As New Thing
So, you instantiate the class to create an object and assign that to a variable.

In your project you have a DataSet class. You only have one DataSet class because you only have one database. The only time you would have more than one DataSet class is if you had more than one data source. When you drag it onto a form in the designer, the IDE adds code that declares a variable, creates an instance of the class and assigns that object to the variable. If you do that with two different forms then you have two different variables that refer to two different objects. If the forms act on different data then that is completely appropriate.

If the two forms act on the same data then only one of them should create a DataSet object. Rather than the other form creating a second DataSet object, the first form passes the object it created to the second form. You will then have two different variables in the two different forms but they will both refer to the same object.
Alright! I think I got it ;)
if I have 2 Data Sources I would have 2 DataSets for each data source, is that correct ;)?
 
Back
Top