Link to home
Start Free TrialLog in
Avatar of Declan Basile
Declan BasileFlag for United States of America

asked on

Why does the data adapter work but the reader does not?

If this works

        Dim strRootNodeName As String
        Dim dt As New DataTable

        Using cn As New SqlConnection(GlobalVariables.strConn)
            Using cmd As New SqlCommand(m_strNameLookupSQL, cn)
                cmd.Parameters.Add("@ItemId", SqlDbType.Int).Value = m_RootNodeId
                Using da As New SqlDataAdapter(cmd)
                    da.Fill(dt)
                    strRootNodeName = dt.Rows(0)("ItemName")
                End Using
            End Using
        End Using

Why would this not work?

        Dim strRootNodeName As String

        Using cn As New SqlConnection(GlobalVariables.strConn)
            Using cmd As New SqlCommand(m_strNameLookupSQL, cn)
                cmd.Parameters.Add("@ItemId", SqlDbType.Int).Value = m_RootNodeId
                Using rdr = cmd.ExecuteReader
                    rdr.Read()
                    strRootNodeName = rdr("ItemName")
                End Using
            End Using
        End Using
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Declan Basile

ASKER

Excellent!  Thank you.
Not a problem Declan_Basile, glad to help.