I have a property active to indicate status of an employee. Along with all the details, i extract the details from an employee table into a dataset, but however i am not able to get the value present in active as i get an invalid cast exception.

This is the property declaration.

P

rivate Active As SqlTypes.SqlBoolean
        Public Property Actv() As SqlTypes.SqlBoolean
            Get
                Return Active
            End Get
            Set(ByVal value As SqlTypes.SqlBoolean)
                Active = value
            End Set
        End Property

This is the data access code , i get an error when in the while loop , i add the details into the employee object. Please fill me in on any mistakes that i have made.

Public Function GetEmployeeDetails(ByVal lname As String) As List(Of Employee)
        Try
            Dim con As New SqlConnection
            con.ConnectionString = constring
            con.Open()
            Dim sql As String = "select * from Employee where LoginName = '" + lname + "'"
            Dim com As New SqlCommand(sql, con)   ' Declaring a command object

            Dim dr As SqlDataReader = com.ExecuteReader()
            Dim lstEmp As New List(Of Employee)

            If dr.HasRows Then

                While dr.Read()
                    Dim emp As New Employee With {.EmpID = dr.GetInt32(0), .EmpName = dr.GetString(1), .Bdate = dr.GetDateTime(2),
                                                  .DOJ = dr.GetDateTime(3), .LogName = lname, .pswd = dr.GetString(5), .DeptID = dr.GetInt32(6),
                                                  .Addr = dr.GetString(7), .Bsc = dr.GetInt32(8), .Tallow = dr.GetInt32(9),
                                                  .Hrallow = dr.GetInt32(10), .Actv = dr.GetInt32(11)}
                    lstEmp.Add(emp)
                End While
                dr.Close()
                con.Close()
                Return lstEmp
            Else
                Return lstEmp
            End If
        Catch ex As SqlException
            Throw ex

        End Try

    End Function

change below in you code:

.Actv = dr.GetBoolean(11)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.