Jump to content

insert values in listbox from database table

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
yasinirshad

yasinirshad

    Newbie

  • Members
  • Pip
  • 3 posts
Hi,
I have two forms, form1 and form2. In form1 i have a textbox & a button which onclick opens form2. in form2 i have a listbox.when a user selects a value from listbox in form2(manually inserted few items in designer) , this corresponding value gets populated on textbox in form1. This works fine and below is my code.
Form1:

Public FR As frmNCNtest = Nothing

<STAThread()> _

    Private Shared Sub main()

        Application.Run(New frmNCNpopup)

    End Sub

Private Sub btn_openchild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_openchild.Click

        FR = New frmNCNtest

        FR.ShowDialog(Me)

    End Sub


    Private Sub frmNCNpopup_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        If Not FR Is Nothing Then

            txt_test3.Text = FR.ListBox1.SelectedItem.ToString()

            FR.Dispose()

        End If

    End Sub
Form2:

 Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Me.Close()

    End Sub
Now how to insert items at runtime from database in listbox on form 2. I tried following code, but it only inserts forst item ,even that not in listbox on form2 but on textbox in form1. below is code:
Form2:

Private Sub abc()

        Try

            If ConState = False Then

                Connect()

            End If

            strSQLtest = SqlQry.qryNCNreason

            Dim cmd1 As New OleDbCommand(strSQLtest, dbConn)

            Dim datareader As OleDbDataReader = cmd1.ExecuteReader()

            Dim list As New ArrayList

            If datareader.HasRows Then

                While datareader.Read()

                    list.Add(datareader.GetString(0))

                End While

            End If

            Me.ListBox1.DataSource = list

        Catch ex As OleDbException

            MessageBox.Show(ex.Message, "Database read error!", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub
Can anyone pls help me how to insert values in listbox from database table and whats wrong here. Thanks.

#2
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
I think this is VB6.0 Right? If yes then please zip and upload the whole project. (including the db if it has no secret/sensitive data)

Thanks