Click here to Skip to main content
15,915,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im having a problem on how to insert the listed items from a listbox to database and retreive it late?

What I have tried:

This is my code inserting database, how could i insert items of a listbox?

VB
Try
           connect()
           connection.Open()
           Dim queryString As String
           queryString = "Insert into ServiceRecords([ServiceID],[ClientName],[Contact Person],[Address],[TimeStarted],[TimeEnded],[OpDate],[FloorDrain Quantity],[FloorDrain Clogged],[FloorDrain F/R]) Values(@SeriveID,@ClientName,[@Contact Person],@Address,@TimeStarted,@TimeEnded,@OpDate,[@FloorDrain Quantity],[@FloorDrain Clogged],[@FloorDrain F/R])"
           command = New OleDbCommand(queryString, connection)
           command.Parameters.Add(New OleDbParameter("ServiceID", CType(serviceIDText.Text, String)))
           command.Parameters.Add(New OleDbParameter("ClientName", CType(clientNameText.Text, String)))
           command.Parameters.Add(New OleDbParameter("Contact Person", CType(cPersonText.Text, String)))
           command.Parameters.Add(New OleDbParameter("Address", CType(addressText.Text, String)))
           command.Parameters.Add(New OleDbParameter("TimeStarted", CType(timeStartedText.Text, String)))
           command.Parameters.Add(New OleDbParameter("TimeEnded", CType(timeEndedText.Text, String)))
           command.Parameters.Add(New OleDbParameter("OpDate", CType(datePicker.Text, Date)))
           command.Parameters.Add(New OleDbParameter("FloorDrain Quantity", CType(fldQuantityText.Text, String)))
           command.Parameters.Add(New OleDbParameter("FloorDrain Clogged", CType(fldClcbox.CheckState, Boolean)))
           command.Parameters.Add(New OleDbParameter("FloorDrain F/R", CType(fldFinRecText.Text, String)))

           MsgBox("Successfully Added!", MsgBoxStyle.Information)
           Me.Close()

           command.ExecuteNonQuery()
           displayDataToGrid()
           mainForm.serviceRecordDataGridView.Rows(mainForm.serviceRecordDataGridView.Rows.Count - 1).Selected = True
           connection.Close()


       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try
Posted
Updated 4-Feb-18 4:30am
Comments
OriginalGriff 4-Feb-18 9:49am    
What have you tried?
Where are you stuck?
What help do you need?

You have the code to do it from textboxes, so what is your problem with doing it from a ListBox?
WinterPrison 6-Feb-18 2:30am    
i have try a solution and it worked, but only the 1st item is inserted. can u help me?

Try
connect()
connection.Open()
Dim queryString As String
queryString = "Insert into ServiceRecords([Personnel]) Values(@Personnels)"
command = New OleDbCommand(queryString, connection)
For i As Integer = 0 To Me.ListBox1.Items.Count + 1
command.Parameters.AddWithValue("@Personnels", ListBox1.Items(i))
Next
Catch ex As Exception

End Try

1 solution

Take a look at your code:
VB
MsgBox("Successfully Added!", MsgBoxStyle.Information)
Me.Close() 'HERE!!!

command.ExecuteNonQuery()

ExecuteNonQuery will never reached because a form is closed first.
 
Share this answer
 
Comments
WinterPrison 4-Feb-18 22:24pm    
Im sorry, this is my first coding language so i dont know much :(, please help me
Maciej Los 5-Feb-18 1:36am    
Please, read my answer carefully and you'll able to resolve your issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900