List all the data in a textbox from DB

charles_cp

New member
Joined
Jul 29, 2005
Messages
1
Programming Experience
Beginner
Dear,
I would like to list all of the email address in the TextBox1 from the DB. But the following program just show the first row of the data ("tony@aol.com, "). How to make the TextBox1 showing like that "tony@aol.com, peter@hotmail.com, judy@yahoo.com" ? Please help!




My Code:
--------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Oledb1 As New OleDbConnection
Oledb1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\cus.mdb"
Oledb1.Open()
If Oledb1.State.Open Then
Dim Cmd1 As New OleDbCommand
Cmd1.Connection = Oledb1
Cmd1.CommandText = "Select email&', ' From customer"
TextBox1.Text = Cmd1.ExecuteScalar()
Oledb1.Close()
End If
End Sub
 
ExecuteScalar always returns one record only ... remember that fact :)

Your code should look as it follows:
VB.NET:
[color=blue]Dim[/color] customerEmail[color=blue] As[/color] String
[color=blue]Dim[/color] strSQL [color=blue]As[/color] String = "SELECT email FROM customer"
[color=blue]Dim[/color] cmd [color=blue]As[/color] OledbCommand = [color=blue]New[/color] oledbcommand(strSQL, Oledb1)
[color=blue]Dim[/color] objread [color=blue]As[/color] OledbReader = cmd.ExecuteReader
 
[size=2][color=#0000ff]While[/color][/size][size=2] objRead.Read
customerEmail &= objRead("eMail") & ","
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]While
 
[/color][/size][size=2]objRead.Close()
 
[size=2][color=#0000ff]Me[/color][/size][size=2].txtCcMail.Text = customerEmail
[/size]

And finally you can substring last comma from there ... feel free to ask if you need an additional sugesstion

Cheers ;)
[/size]
 
Back
Top