10 Replies - 8870 Views - Last Post: 28 August 2011 - 12:10 PM Rate Topic: -----

#1 shehzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-March 11

how to add items into listbox

Posted 24 August 2011 - 12:52 PM

i have a listbox in form1. i am adding contents into the database in form2. i want to update the listbox in form1 with the newly added items in form2. i am using vb.net n microsoft sql server. can anyone please help me.
thanks in advance
Is This A Good Question/Topic? 0
  • +

Replies To: how to add items into listbox

#2 shehzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-March 11

Re: how to add items into listbox

Posted 24 August 2011 - 01:05 PM

i tried
Form1.ListBox1.Items.Add(TextBox2.Text)

it is getting updated but once i close the application and run again, that is lost, its not getting saved
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: how to add items into listbox

Posted 24 August 2011 - 01:33 PM

What you have to do is populating your List box directly from database. And when a new item is added, just re-populate(refresh query). Just like what you did to read rate in your previous thread, except you will have to update list box values like:
            While myreader.Read()
                ListBox1.BeginUpdate()
                ListBox1.Items.Add(myreader.Item("ColumnName"))
                ListBox1.EndUpdate()
            End While
            myreader.Close()

Was This Post Helpful? 0
  • +
  • -

#4 shehzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-March 11

Re: how to add items into listbox

Posted 24 August 2011 - 01:59 PM

what correction is required in this?

            Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        myconnection = New SqlConnection("server=NXT\SQLEXPRESS;uid=;pwd=;database=shop;Integrated security=true")
        sql = "INSERT INTO stock VALUES('" & TextBox2.Text & "','" & ListBox3.SelectedItem & "','" & TextBox3.Text & "')"
        myconnection.Open()
        adapter.InsertCommand = New SqlCommand(sql, myconnection)
        adapter.InsertCommand.ExecuteNonQuery()
        MsgBox("Member added successfully...!! ")

        dr = mycommand.ExecuteReader()

        While dr.Read()
            ListBox1.BeginUpdate()
            ListBox1.Items.Add(dr.Item("base"))
            ListBox1.EndUpdate()
        End While
        dr.Close()


    End Sub

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: how to add items into listbox

Posted 24 August 2011 - 02:10 PM

You have to put your code when your form is loaded(the one with the list box):
'first you will have your query
sql = "SELECT ColumnName FROM stock"
'Now after filling your data reader, you will put the above code snip which I posted

Was This Post Helpful? 0
  • +
  • -

#6 shehzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-March 11

Re: how to add items into listbox

Posted 24 August 2011 - 02:23 PM

i did this
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        myconnection = New SqlConnection("server=NXT\SQLEXPRESS;uid=;pwd=;database=shop;Integrated security=true")
        sql = "INSERT INTO stock VALUES('" & TextBox2.Text & "','" & ListBox3.SelectedItem & "','" & TextBox3.Text & "')"
        myconnection.Open()
        adapter.InsertCommand = New SqlCommand(sql, myconnection)
        adapter.InsertCommand.ExecuteNonQuery()
        MsgBox("Member added successfully...!! ")

        mycommand = New SqlCommand("SELECT base FROM stock", myconnection)

        dr = mycommand.ExecuteReader()

        While dr.Read()
            ListBox1.BeginUpdate()
            ListBox1.Items.Add(dr.Item("base"))
            ListBox1.EndUpdate()
        End While
        dr.Close()


    End Sub

the prob is the listbox shows all the items wen i update, but after i close n reopen the app, there is no change again wen i modify i can see d changes i created last tym
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: how to add items into listbox

Posted 24 August 2011 - 02:36 PM

That is why I said this code should also be in the load event of the form where list box is in:
 mycommand = New SqlCommand("SELECT base FROM stock", myconnection)

        dr = mycommand.ExecuteReader()

        While dr.Read()
            ListBox1.BeginUpdate()
            ListBox1.Items.Add(dr.Item("base"))
            ListBox1.EndUpdate()
        End While
        dr.Close()

Was This Post Helpful? 1
  • +
  • -

#8 shehzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-March 11

Re: how to add items into listbox

Posted 25 August 2011 - 12:33 AM

View Postsmohd, on 25 August 2011 - 03:06 AM, said:

That is why I said this code should also be in the load event of the form where list box is in:
 mycommand = New SqlCommand("SELECT base FROM stock", myconnection)

        dr = mycommand.ExecuteReader()

        While dr.Read()
            ListBox1.BeginUpdate()
            ListBox1.Items.Add(dr.Item("base"))
            ListBox1.EndUpdate()
        End While
        dr.Close()

thanks a lot :) it work :)
god bless ya :)
Was This Post Helpful? 0
  • +
  • -

#9 shehzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-March 11

Re: how to add items into listbox

Posted 27 August 2011 - 01:01 PM

i want to avoid repetitions when updating a listbox, can u help me
thnx in adance
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: how to add items into listbox

Posted 28 August 2011 - 09:34 AM

First you have to clear everything using ListBox1.Items.Clear()
Was This Post Helpful? 0
  • +
  • -

#11 shehzi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-March 11

Re: how to add items into listbox

Posted 28 August 2011 - 12:10 PM

but wont there be any problem if the list i want is lengthy? actually i am another listbox with some denominations concerning the first listbox. each item in the first listbox have many denominations given in the second listbox. say im storing an item in d first listbox, its denominations ie quantity is stored in the second list box, so basically while adding an item, i hav to add the same item with different denominations so the no.of items in the listbox1 increases.
i tried searching the databse for similar names and then updating the listbox, but there is some mistake in what i coded coz i didnt get d expected output.
 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        myconnection = New SqlConnection("server=NXT\SQLEXPRESS;uid=;pwd=;database=paints;Integrated security=true")
        myconnection.Open()
        mycommand = New SqlCommand("select count(*) from stock where base='" & TextBox2.Text & " ' AND quantity='" & ListBox3.SelectedItem & "'")


        mycommand.Connection = myconnection
        ra = mycommand.ExecuteScalar
        If (ra > 0) Then
            MsgBox("The product already exists ", MsgBoxStyle.Exclamation)
            TextBox2.Clear()
            TextBox2.Focus()
            TextBox3.Clear()

        Else

            myconnection = New SqlConnection("server=NXT\SQLEXPRESS;uid=;pwd=;database=paints;Integrated security=true")
            sql = "INSERT INTO stock VALUES('" & TextBox2.Text & "','" & ListBox3.SelectedItem & "','" & TextBox3.Text & "')"
            myconnection.Open()
            adapter.InsertCommand = New SqlCommand(sql, myconnection)
            adapter.InsertCommand.ExecuteNonQuery()
            MsgBox("Member added successfully...!! ")

            
            mycommand = New SqlCommand("select count(*) from stock where base='" & TextBox2.Text & " ' AND quantity='" & ListBox3.SelectedItem & "'")


            mycommand.Connection = myconnection
            ra = mycommand.ExecuteScalar
            If (ra > 0) Then
                TextBox2.Clear()
            Else

                mycommand = New SqlCommand("SELECT base FROM stock", myconnection)
                dr = mycommand.ExecuteReader()

                While dr.Read()

                    ListBox1.BeginUpdate()
                    ListBox1.Items.Add(dr.Item("base"))
                    ListBox1.EndUpdate()
                    Form1.ListBox1.BeginUpdate()
                    Form1.ListBox1.Items.Add(dr.Item("base"))
                    Form1.ListBox1.EndUpdate()
                End While
                dr.Close()
            End If
        End If
        TextBox3.Clear()
        TextBox2.Clear()
        ListBox3.ClearSelected()



    End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1