 |
BOOK: Beginning ASP.NET 2.0 and Databases  | This is the forum to discuss the Wrox book Beginning ASP.NET 2.0 and Databases by John Kauffman, Bradley Millington; ISBN: 9780471781349 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 2.0 and Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

September 30th, 2006, 04:05 AM
|
|
Registered User
|
|
Join Date: Sep 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My VB Code for Ch14
Take this with a grain of salt because I've been up all night and I'm kinda tired! But I went back through Chapter 14 and re-did all the code in Visual Basic just to see if I could do it. Below is my code for the two classes the book asks you to build.
I kinda ran out of gas here and stopped a little short of the end, but it's got everything up through 1407 and the last couple (if I remember from doing them in C-Sharp) work pretty much the same way.
Any questions/comments are welcome. I can also be reached at [email protected].
First my Person. VB class:
Code:
Imports Microsoft.VisualBasic
Imports System
Public Class Person
Private _Id As Integer
Private _FirstName As String
Private _LastName As String
Private _Title As String
Private _City As String
Public Property ID() As Integer
Get
Return _Id
End Get
Set(ByVal value As Integer)
_Id = value
End Set
End Property
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Public Property City() As String
Get
Return _City
End Get
Set(ByVal value As String)
_City = value
End Set
End Property
Public Sub New(ByVal ID As Integer, ByVal FirstName As String, ByVal LastName As String, ByVal Title As String, ByVal City As String)
_Id = ID
_FirstName = FirstName
_LastName = LastName
_Title = Title
_City = City
End Sub
End Class
And below is my take on MyDataObject. vb:
Code:
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports NorthwindDataSetTableAdapters 'Uncomment this line only after building the dataset for TIO-1405
Imports Microsoft.VisualBasic
Public Class MyDataObject
'For TIO-1401 (Page 355)
Public Function GetLastNameOneOnly()
Dim LastNames As New ArrayList(0)
LastNames.Add("Millington")
Return LastNames
End Function
'For TIO-1402 (Page 357)
Public Function GetLastNames()
Dim LastNames As New ArrayList(3)
LastNames.Add("Millington")
LastNames.Add("Kauffman")
LastNames.Add("Washington")
LastNames.Add("Lincoln")
Return LastNames
End Function
'For TIO-1403 (Page 363)
Public Function GetPeopleList()
Dim people As New List(Of Object)
Dim p1 As New Person(74, "Brad", "Millington", "Program Mgr", "Redmond")
Dim p2 As New Person(58, "John", "Kauffman", "Author", "Ottawa")
Dim p3 As New Person(68, "George", "Washington", "Developer", "Redmond")
Dim p4 As New Person(79, "Abraham", "Lincoln", "Developer", "Washington")
people.Add(p1)
people.Add(p2)
people.Add(p3)
people.Add(p4)
Return people
End Function
'For TIO-1404 (Page 367)
Public Function GetPeopleDataSet() As DataSet
Dim connStr As String = ConfigurationManager.ConnectionStrings("MdfNorthwind").ConnectionString
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim query As String = "Select EmployeeID, LastName, FirstName, Title, City from Employees"
Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, conn)
Dim ds As DataSet = New DataSet
adapter.Fill(ds)
Return ds
End Function
'For TIO-1406 (Page 374)
Public Function GetPeopleListFromDataSet()
Dim people As New List(Of Object)
Dim adapter As New EmployeesTableAdapter
Dim table As NorthWindDataSet.EmployeesDataTable = adapter.GetEmployeesByCity
Dim row As NorthWindDataSet.EmployeesRow
For Each row In table
Dim p As Person = New Person(row.EmployeeID, row.FirstName, row.LastName, row.Title, row.City)
people.Add(p)
Next
Return people
End Function
'For TIO-1407 (Page 377)
Public Function UpdatePerson(ByVal ID As Integer, ByVal FirstName As String, ByVal LastName As String, ByVal Title As String, ByVal City As String)
Dim adapter As New EmployeesTableAdapter
Return adapter.Update(ID, FirstName, LastName, Title, City)
End Function
End Class
|
|

September 30th, 2006, 08:51 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
WOW,
Pat--we must be setting similar pace on this book (although I've been skipping through this book a little bit because I really want to learn about business layer). I will give the book this---I finally understand what the heck objectDataSource is for! LOL.
I was extremely dissapointed with the CS code. I, too, also started writing my own VB code..but only had started the the first two "MyDataObject" Hardcoding examples.
Kudos to you for doing this, however!!!
:)
Rob
|
|

September 30th, 2006, 09:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Pat,
Great job--just noticed that your code has 1401 & 1402. I put my functions below--just to compare and provide different perspectives..yours is great:
Public Function GetLastNameOneOnly() As String()
Dim lastNames As String() = {"Milligan"}
Return lastNames
End Function
Public Function GetLastNames() As String()
Dim lastNames As String() = {"Millington", "Kauffman", "Washington", "Lincoln"}
Return lastNames
End Function
|
|

September 30th, 2006, 09:16 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Pat,
I think we're the only two people in here!!
Question--in your third example, what is the difference between what you did, and what I was inclined to do:
Dim people As New List(Of Object) compared to
Dim people As New List(Of Person)
My thinking was that with your way, I could include in the list multiple types of Objects (people and strings) but that didn't work.
Perhaps so that you can dynamically "type" the list when you add items?
If that is the case, I assume once you start adding items, you can't add different types of items to the list?
Pat--thanks for your work with this!!
Kind Regards,
Rob
|
|

October 1st, 2006, 02:51 AM
|
|
Registered User
|
|
Join Date: Sep 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Cool, I didn't realize you could put multiple entries for a string in curly braces like that.
Regarding this line:
Dim people As New List(Of Object) compared to
Dim people As New List(Of Person)
The reason I put "Object" there was simply because I didn't realize I could put "Person" there. I originally put String there and ran into a typing problem, so I switched it to Object and it worked. Logically Person makes more sense in that position -- that's probably more like what the authors were thinking. This is my first experience with Generics (which of course are new to 2005) so I'm learning this one as I go.
So thanks for two good tips there!
I'll be teaching a new crop of ASP.NET students beginning Monday (our first time teaching 2.0), and we'll be using this book as our text (errors aside I still think it's the best book I've seen for beginners). I'll probably spend some time compiling a list of errata as we go along, and also rehash this Ch14 VB code when we get to this spot in the book. If we get all that done I'll be sure and post a link to the info here on this board so others can use it.
|
|

October 1st, 2006, 09:03 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Pat,
Didn't realize you were a teacher. I just graduated college with an IT degree and am SO dissapointed. 90% of my "IT" work was learning C++ from a commandline...NOTHING real world. So, here I am-graduated-and purchasing 5 Wrox books so that I can try to teach myself how to interact with Windows and write useful apps. I am trying to learn the tools to design an app for the church.
I do have a question for you regarding your code for try it out on 377. When I do:
Return adapter.Update(ID, FirstName, LastName, Title, City)
I get an error about the "Update" function not having those parameters. I look at my tableAdapter and all I have is one function "GetData". I did click on the check for "Create methods to send updates...." in the configure section of the adapter..but I don't get anything but my one retrieval method. I do see, however, in the properties and update method..but it says:
UPDATE Employees
SET LastName = @LastName, FirstName = @FirstName, Title = @Title, TitleOfCourtesy = @TitleOfCourtesy, BirthDate = @BirthDate,
HireDate = @HireDate, Address = @Address, City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country,
HomePhone = @HomePhone, Extension = @Extension, Photo = @Photo, Notes = @Notes, ReportsTo = @ReportsTo, PhotoPath = @PhotoPath
WHERE (EmployeeID = @Original_EmployeeID)
Which is obviously different.
How did you get this one to work?
Regards,
Rob
|
|

October 1st, 2006, 09:40 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Ok--got it. If you limit your update function to those fields, I was able to fix it two ways.
1) Go back into configure and add my own "update" query with only updating the (LastName, FirstName, City, Title) fields where ID=ID.
Then, I have to remove all columns from the Gridview except those in the update.
2) Do same as above, except..if I show columns not in the update, I have to change the "readonly" property of that column not in the SQL Update command to true. Therefore, it won't try changing those commands when you update.
I was able to get both to work.
Again, however, the book doesn't say ANYTHING about updating the "UPDATE" command. If you do it like the book says, it generates a default "UPDATE" command for every field in the table. The function you write in "MyDataSource" (and the properties of the PERSON class) do not account for anything other than ID, First/Last Name..Title & City.
:)
Rob
|
|

October 1st, 2006, 10:59 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Ok, I worked the rest of the Try It Outs and have code to add to Pat's. I do have a question for anyone willing to help (if anyone but Pat and myself view this forum). In VB, Pat and I have declared functions that are not typed. Up until now, I thought that VB functions had to have a type. For example,
Public Function myFunction() as String
return String
End Function
However, alot of the functions on this page don't have a type and run fine. WHY IS THAT?
For TIO-1408
Public Function GetCities()
Dim cities As New List(Of String)
Dim adapter As New CitiesTableAdapter
Dim table As NorthWindDataSet.CitiesDataTable = adapter.GetCities
For Each row As NorthWindDataSet.CitiesRow In table
cities.Add(row.City)
Next
Return cities
End Function
Public Function GetPeopleByCity(ByVal city As String)
Dim people As New List(Of Person)
Dim adapter As New EmployeesTableAdapter
Dim table As New NorthWindDataSet.EmployeesDataTable
table = adapter.GetEmployeesByCity(city)
For Each row As NorthWindDataSet.EmployeesRow In table
Dim p As New Person(row.EmployeeID, row.FirstName, row.LastName, row.Title, row.City)
people.Add(p)
Next
Return people
End Function
For TIO-1409
Public Function GetPeopleSorted(ByVal sortExpression As String)
Dim people As New List(Of Person)
Dim adapter As New EmployeesTableAdapter
Dim table As New NorthWindDataSet.EmployeesDataTable
table = adapter.GetEmployees()
Dim view As New DataView(table)
view.Sort = sortExpression
For Each row As DataRowView In view
Dim p As New Person(row("EmployeeID"), row("FirstName"), row("LastName"), row("Title"), row("City"))
people.Add(p)
Next
Return people
End Function
|
|

April 17th, 2007, 02:34 PM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for all of your time/effort on this subject Pat, I was frustrated when I found out there was no VB code in the book and WROX didn't offer any via download. While doing through this chapter, I had a couple of questions that I was hoping you could answer.
Quote:
quote:Originally posted by PatFrank
Code:
Imports Microsoft.VisualBasic
'For TIO-1404 (Page 367)
Public Function GetPeopleDataSet() As DataSet
Dim connStr As String = ConfigurationManager.ConnectionStrings("MdfNorthwind").ConnectionString
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim query As String = "Select EmployeeID, LastName, FirstName, Title, City from Employees"
Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, conn)
Dim ds As DataSet = New DataSet
adapter.Fill(ds)
Return ds
End Function
|
In the above function you specify a return type (Public Function GetPeopleDataSet() As DataSet), but not in the other functions, why?
Quote:
quote:
Code:
'For TIO-1406 (Page 374)
Public Function GetPeopleListFromDataSet()
Dim people As New List(Of Object)
Dim adapter As New EmployeesTableAdapter
Dim table As NorthWindDataSet.EmployeesDataTable = adapter.GetEmployeesByCity
Dim row As NorthWindDataSet.EmployeesRow
For Each row In table
Dim p As Person = New Person(row.EmployeeID, row.FirstName, row.LastName, row.Title, row.City)
people.Add(p)
Next
Return people
End Function
|
Wouldn't this one be: Public Function GetPeopleListFromDataSet() As List(Of Person)? Well, if you made the second line:
Dim people as New List(Of Person)
Quote:
quote:
Code:
'For TIO-1407 (Page 377)
Public Function UpdatePerson(ByVal ID As Integer, ByVal FirstName As String, ByVal LastName As String, ByVal Title As String, ByVal City As String)
Dim adapter As New EmployeesTableAdapter
Return adapter.Update(ID, FirstName, LastName, Title, City)
End Function
|
Wouldn't this one be?:
Public Function UpdatePerson(ByVal ID As Integer, ByVal FirstName As String, ByVal LastName As String, ByVal Title As String, ByVal City As String) As adapter
Thanks.
|
|
 |