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
how to add items into listbox
Page 1 of 110 Replies - 3652 Views - Last Post: 28 August 2011 - 12:10 PM
Replies To: how to add items into listbox
#2
Re: how to add items into listbox
Posted 24 August 2011 - 01:05 PM
i tried
it is getting updated but once i close the application and run again, that is lost, its not getting saved
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
#3
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()
#4
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
#5
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
#6
Re: how to add items into listbox
Posted 24 August 2011 - 02:23 PM
i did this
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
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
#7
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()
#8
Re: how to add items into listbox
Posted 25 August 2011 - 12:33 AM
smohd, 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
god bless ya
#9
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
thnx in adance
#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()
#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.
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|