Link to home
Start Free TrialLog in
Avatar of andyb7901
andyb7901

asked on

DB into combo field

Can someone simply tell me how to add data to a combo box from my DB? The connection I am using is something like this;

                Sql = "SELECT * from tbl_User_Problem WHERE Problem_ID = " & Cell_ID ' Set up connection
                Dim cmd As New OleDbCommand(Sql, conn) ' Concatinate with connection string
                Dim dr As OleDbDataReader = cmd.ExecuteReader

               While dr.Read ' Read contents for designtaed rows into Global Variables
                         GlobProblem_ID = dr.Item("Problem_ID")
Avatar of andyb7901
andyb7901

ASKER

I have used the follwing which has been succesful. Could someone tell me though how to add columns to it, if it can be done? So I can get a users ID along with their name fromthe same column?

cmbUsers.Items.Add(dr.Item("UserName"))
   

try this:
' Just Send ur comboBox & Cell_ID
' Dont forget the NameFld
Public Sub FillCombo(ByRef cBox As ComboBox,ByVal Cell_ID As String)

        Try
            Dim Sqls As String = "SELECT * from tbl_User_Problem WHERE Problem_ID = " & Cell_ID
            Dim Tbl As String = "tbl_User_Problem"
            Dim NameFld As String = " *** "
            Dim IdFld As String = "Problem_ID"

            Dim Conn As SqlConnection = New SqlConnection(v_ConnectionString)
            Dim Comm As SqlCommand = New SqlCommand(Sqls, Conn)
            Comm.Connection = Conn
            Comm.CommandText = Sqls
            Comm.CommandType = CommandType.Text '.StoredProcedure
            Dim DS As DataSet = New DataSet()
            Dim DA As New SqlDataAdapter(Comm)

            Conn.Open()
            DA.Fill(DS, Tbl)

            Dim dt As New DataTable
            dt.Columns.Add(NameFld, GetType(System.String))
            dt.Columns.Add(IdFld, GetType(System.String))

            With cBox
                .DataSource = DS.Tables(Tbl)    'dt
                .DisplayMember = NameFld
                .ValueMember = IdFld
            End With

            Conn.Close()

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

Hop this Help
I cant get this to work? It doesnt like the New SqlDataAdapter section and these two lines. Could my addiotnal code be adapted?

        Dim Sql As String

        cmbLocation.Text = ("Head Office")

        Sql = "SELECT * from tbl_StaffIT" ' Set up connection

        Dim cmd As New OleDbCommand(Sql, conn) ' Concatinate with connection string

        Dim dr As OleDbDataReader = cmd.ExecuteReader

        While dr.Read ' Read contents for designtaed rows into Global Variables

            cmbUsers.Items.Add(dr.Item("UserName"))

        End While

        cmbUsers.Text = ("Select Name")
ASKER CERTIFIED SOLUTION
Avatar of hatem72
hatem72

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have used the following and seems ok. But havent a clue whats going on. could you tell me?

Imports System.Data.OleDb

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        conn.Open()

        Try

            Dim dbCommand As New OleDbCommand("SELECT * from tbl_StaffIT", conn)
            Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader

            ComboBox1.BeginUpdate()

            Do While dbDR.Read

                ComboBox1.Items.Add(New ListBoxItem(CInt(dbDR("UserID")), dbDR("UserName").ToString))

            Loop

            ComboBox1.EndUpdate()

        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error")
        End Try

    End Sub
End Class


Public Class ListBoxItem

    Private listItemData As Object
    Private listItemText As String

    ' This is what is displayed in the ComboBox drop-down
    Public Overrides Function ToString() As String
        Return listItemText
    End Function

    Public Sub New(ByVal itemData As Object, ByVal itemText As String)

        listItemData = itemData
        listItemText = itemText

    End Sub

    Public ReadOnly Property Data() As Object

        Get

            Data = listItemData

        End Get

    End Property

    Public ReadOnly Property Text() As String
        Get
            Text = listItemText
        End Get

    End Property
is there any error msg ???
No it works perfectly, but I am unexperienced with this code, so wanted to know what the code was doing? Commented?
its something like Create New Data Type By Create Class consist of two fields  

    Private listItemData As Object ' Define Field for store the ID
    Private listItemText As String ' Define Field to store Text

the Purpose of the class is combine the ID And TEXT
and then return them by using Class Propertys ( Data,Text ) .

check this link :
http://msdn.microsoft.com/vstudio/express/beginner/windows/tier2/begin6/vb/
http://msdn.microsoft.com/vstudio/express/beginner/learningpath/default.aspx

i hope this help ,
So if I wanted to take the ID of the currently selected record how would I do that?

try this :

        For i As Integer = 0 To Me.ComboBox1.Items.Count - 1
            Me.ComboBox1.SelectedIndex = i
            Dim value As String
            Dim Text As String

            value = CType(ComboBox1.SelectedItem, ListBoxItem).Data.ToString ' Get ID
            Text = CType(ComboBox1.SelectedItem, ListBoxItem).Text.ToString ' Get Text
            Debug.Print("Text : " & Text & " ID:" & value)
        Next

i hope this help ,
Where would I put this? In an on-click event. If so it just seems to be looping the names through the combo in an ever lasting loop. Also, it didnt like the .data and .text bits. Would this work;

            value = CType(ComboBox1.SelectedItem, ListBoxItem).ToString ' Get ID
            Text = CType(ComboBox1.SelectedItem, ListBoxItem).ToString ' Get Text

Hi andyb7901,

did u try the above code ... it works fine ... about where to put ur code i dont know .
And that loop just for demonstration .