Returning a Class

Status
Not open for further replies.

ConnyD

Member
Joined
Oct 1, 2006
Messages
8
Programming Experience
1-3
Can anyone help? I'm reading through a book Pro ASP.NET 2.0 in VB 2005 and I have come across a problem returning a class in a fucntion call. I call the function GetEmployee from a page load event handler passing it an integer argument which is used in a SQL stored procedure to return a single row from a employees table. Employee is declared as a obejct of class EmployeeDetails which contains the following member variables: EmployeeID, FirstName, LastName, TitleOfCourtesy. When I run the code I'm getting an exception. Can anyone help?

Creating database component
VB.NET:
Private db as EmployeeDB = New DatabaseComponent.EmployeeDB()
 
'The following code is contained in a page load event handler.
.
.
.
Dim Employee as EmployeeDetails = New EmployeeDetails
db.GetEmployee(1)
lblFirstName.Text = Employee.FirstName
.
.
.
End Sub
 
Public Function GetEmployee(ByVal EmployeeID as Integer) AS EmployeeDetails
Dim con As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("sp_GetEmployee",con)
cmd.CommandType = CommandType.StoredProcedure
cmd.parameters.Add(New SqlParameter("@EmployeeID",SqlDbType.Int, 4))
cmd.Parameters("@EmployeeID").Value = EmployeeID
Try
con.open
Dim reader as SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
reader.Read()
Dim emp as EmployeeDetails = New EmployeeDetails(CInt(reader("EmployeeID")), _
CStr(reader("FirstName")), CStr(reader("LastName")),CStr(reader("TitleOfCourtesy")))
reader.close()
[COLOR=royalblue]return emp //I'm not convinced this is right[/COLOR]
Catch ex as SqlException
Throw New ApplicationException("Date error.")
Finally
con.close()
End Try
End Function
 
Last edited by a moderator:
Please do not say that you're getting an exception without providing details. What type of exception and what is the error message? They don't give you that information for nothing.
 
Where exactly does the error occur?

Wild guess but i think this code is incorrect (i can be wrong, it just seems unlogical)

VB.NET:
Dim Employee as EmployeeDetails = New EmployeeDetails
db.GetEmployee(1)
lblFirstName.Text = Employee.FirstName

shouldn't this be:

VB.NET:
Dim Employee as EmployeeDetails = db.GetEmployee(1)
lblFirstName.Text = Employee.FirstName
 
I've seen it happen before, but jmcilhinney has probably seen too many badly explained questions and wrongly set profiles (VB.NET 1.1 or 2.0) that he's become a little bittered.

I can totally understand that, but the sarcasm, well... that's unnecessary, no need to humiliate people to point out they are doing something wrong.
 
It seems that the nanny state and big brother are in full force on this forum.
Apparently the word similar to sanguine is inappropriate to the moderators, and they choose to then delete my post. All I was stating was that to berate a newbie for providing incomplete information in such a manner was petty and a little over the top. Obviously I am not entitled to voice an opinion on netiquette.
Sorry if I offended anybody but I think the tone of the reply was patronising to say the least, and a trifle rude.
 
This thread has gone completely off the topic of vb.net programming and before it denigrates further i have no option but to lock this thread.
 
Status
Not open for further replies.
Back
Top