selecting 15 records in vb.net RRS feed

  • Question

  • User-1578974752 posted

    Below is my code. I want to check only first 15  records in the select statement.

    select TOP 15 * FROM Detail where TestID ='1209' ORDERBY ASC 

    working in sql but not working in my code

    cmd.CommandText = "select * FROM Detail where TestID = '" + qusetid.Text + "'"

                drd = cmd.ExecuteReader

                If drd.HasRows = True Then

                    While drd.Read
                                       End While
                End If

    Tuesday, November 27, 2018 1:51 AM

All replies

  • User753101303 posted


    Doesn't work that is ? Always tell what actually happens rather than just that "it doesn't work".

    It seems your code doesn't include the TOP 15 option ???? Is this intended ? If you get more than 15 rows, start by updating your SQL statement with the one you actually want !!! If drd.HasRows is not even entered, it is rather that the criteria is wrong.

    Not directly related but do a search for "SQL parameters". You shouldn't build SQL strings this way.

    Tuesday, November 27, 2018 12:48 PM
  • User1120430333 posted

    You are using a datareader So how are you planning to get  data so that you can use it? 

    Tuesday, November 27, 2018 2:07 PM
  • User-1716253493 posted

    Your post title is selecting 15 records in vb.net, but select top 15 only in sql, not in vb

    Tuesday, November 27, 2018 10:09 PM
  • User283571144 posted

    Hi shsu,


    working in sql but not working in my code

    According to your description, I couldn't understand your issue clearly.

    Do you mean if you use this query "select TOP 15 * FROM Detail where TestID ='1209' ORDERBY ASC ", it doesn't return the right data. 

    If you don't know how to get the data by using ado.net, I suggest you could refer to below codes:

            Dim conxnString As String = "YOUR CONNECTION STRING"
            Dim cmdString As String = "select TOP 15 * FROM Detail where TestID ='1209' ORDERBY ASC "
            Using conxn As New SqlConnection(conxnString)
                Using cmd As New SqlCommand(cmdString, conxn)
                     Dim reader As SqlDataReader = cmd.ExecuteReader()
                    While reader.Read()
                           reader(0), reader(1), reader(2))
                    End While
                End Using
            End Using

    Best Regards,


    Wednesday, November 28, 2018 7:32 AM