Working with database and objects?

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
Private Sub btnPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
Dim objCustomer As Customer
Dim objProduct As Products
Try


objCustomer = CType(lstCustomer.SelectedItem, Customer)
objProduct = CType(lstProducts.SelectedItem, Products)

Dim bl As Decimal = objCustomer.Balance
Dim pr As Decimal = objProduct.Price

If bl < pr Then
MessageBox.Show("Your Balance is less then the price of Product: ", "MyShop", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
objCustomer.Balance = bl - pr
MessageBox.Show("You have successfully completed the transaction", "MyShop", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

Catch ex As Exception

End Try
End Sub
End Class

I have two questions

1- why not just write queries those manipulate the database rather then creating objects, making changes and then going back to database.
:confused:

2- now that i have got the new balance and the product oder has been made so every time this happens a query has to be sent to change database or there is any other better why of doing it?
 
Umm

I'm not sure I see examples of your questions in the code you posted, but any way here are my reasons.

I use to place all my code together in the same form code behind page. I thought that it seems silly to call back and forth, however once I started using objects I see the advantages. For one thing it makes your code so much cleaner and easier to follow.

If I were using your example I would have 4 object classes.

1 called clsCustomers that holds all the properties that relate to the customer such as name, address ect.

1 called clsCustomersADO which holds all the database code and functions or subs that I will use to fill in the the clsCustomer properties.

Then 2 more for the products name clsProducts and clsProductsADO.

I'm confused about your second question because if you do not update the database with the purchases then how are you going to know what they ordered? You would not necessarily have to update the DB every single time a product was picked. You can declare multiple product objects and wait until check out to update the DB like this.

Dim objProductA as new clsProduct
Dim objProductB as new clsProduct
Dim objProductC as new clsProduct

Each of these object hold all the Products properties that you fill from the selections like objProductA. Name = "Widget" ect. Then may be you have a function in your ADO class that excepts an array of objects.

Shared Function UpdatePurchases(ByVal objProducts as List(Of clsProducts)) as boolean

This function the holds the code to iterate through each object and run the UPDATE function to the database.

Hope this helps,

Ty
 
Back
Top