Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
VB
dim idname as string
idsql = "select * from xyz "
        With cmd
            dr = .ExecuteReader
        End With

        If dr.HasRows = True Then
            Do While dr.Read
                idname = dr!fname
            Loop
            dr.Close()
        End If
Posted
Comments
lakshjoshi 30-Nov-14 3:04am    
idname = dr!fname//throwing error: indexoutofrangeexception

1 solution

Try this:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("SELECT fname FROM xyz", con)
		Using reader As SqlDataReader = com.ExecuteReader()
			While reader.Read()
				Dim fname As String = DirectCast(reader("fname"), String)
				Console.WriteLine(fname)
			End While
		End Using
	End Using
End Using
 
Share this answer
 
Comments
lakshjoshi 30-Nov-14 2:46am    
Dim fname As String = DirectCast(reader("fname"), String)//throwing error: indexoutofrangeexception
OriginalGriff 30-Nov-14 3:27am    
And what columns do you have in your "xyz" table?
lakshjoshi 30-Nov-14 3:55am    
only fname
OriginalGriff 30-Nov-14 4:19am    
Double check: make sure it'e the right table name, make sure it's the right database, and check that it's the right name = no underscores, no spaces.
Because when I use that code with the correct values locally, it works!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900