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
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: