Rellenar un ComboBox con un DataReader
Artículo por Club Developers · 11 mayo 2006
2618 vistas
Veamos cómo rellenar un ComboBox con un DataReader:
vbnet
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Windows.Forms ' ... Dim con As SqlConnection = Nothing Dim command As SqlCommand = Nothing Dim cs As String = "cadena de conexión SQL" Dim dr As SqlDataReader = Nothing Try con = New SqlConnection(cs) command = New SqlCommand("SELECT campo FROM Tabla",con) ' abrir la conexión con.Open() dr = command.ExecuteReader() ' vaciar ComboBox ComboBox1.Items.Clear() If dr.HasRows Then While dr.Read ComboBox1.Items.Add(dr.GetValue(0)) End While Else MessageBox.Show("No result for your Data", "Infos", MessageBoxButtons.OK, MessageBoxIcon.Information) End If Catch ex As Exception MessageBox.Show(ex.Message) Finally End Try ' ...