Click here to Skip to main content
15,063,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have database with mobile numbers now i want to put them in array list and show them one by one.

I want my output to be: 639057318820 639355514108 639056784959

but my code output is: 639057318820 639057318820 639355514108
639057318820 639355514108
639056784959

What I have tried:

VB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim arrName As New ArrayList()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim rdr As SqlDataReader
        con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=GradingSystemSample;Integrated Security=True;Pooling=False"
        cmd.Connection = con
        con.Open()

        cmd.CommandText = "select numb FROM Table2  "
        rdr = cmd.ExecuteReader
        If rdr.HasRows Then
            While rdr.Read

                arrName.Add(rdr("numb"))
                For Each pno As String In arrName
                    MsgBox(pno)
                Next

            End While

        End If
End sub
Posted
Updated 28-Dec-16 19:53pm

First, there is no need to
rdr.HasRows

as
rdr.Read

will return false when there is no row.
The location of the For Each is questionable. Why put it inside the While and that is the culprit that causes "multi reading". It should be placed after the End While.
   
v4
I'd suggest to correct this way:
VB.NET
rdr = cmd.ExecuteReader
'If rdr.HasRows Then
    While rdr.Read

        arrName.Add(rdr("numb"))
        'For Each pno As String In arrName
            MsgBox(rdr("numb"))
        'Next

    End While


Why? Please, read this: SqlDataReader.Read Method (System.Data.SqlClient)[^]
   

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