how can i save multiple listbox items into a database table

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 7634 Views - Last Post: 18 October 2012 - 06:46 PM Rate Topic: -----

#16 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 12:44 AM

i dont think you understand what a parametarized query is. did you look at the links that i provided?
                  Dim sqlcmd As String = "INSERT INTO Unit Order(Accessories) values (@Acc)"
'@Acc is the name of the parameter
                   Dim cmd As New SqlClient.SqlCommand(sqlcmd, conn)
                   cmd.Parameters.AddWithValue("@Acc", Me.AccessoriesListBox.Items(i)) '@Acc is the name of the parameter Me.AccessoriesListBox.Items(i) is the value of the parameter. 


Was This Post Helpful? 0
  • +
  • -

#17 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 01:10 AM

i remove my exception msg and now i am getting an error saying "incorrect syntax near ''" and "must declare the scalar variable'@basin'"
Was This Post Helpful? 0
  • +
  • -

#18 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 01:45 AM

why saving listbox items be so complicated? :D
Was This Post Helpful? 0
  • +
  • -

#19 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 02:05 AM

if its easy to u why dnt you hlp me solve my issue..we all not genius and i am stil learning
Was This Post Helpful? 0
  • +
  • -

#20 PeterH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 03-September 09

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 02:16 AM

Lets see the insert block code you now have, the error is meaningful and cant be that difficult to locate.
Was This Post Helpful? 0
  • +
  • -

#21 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 02:18 AM

View Postchristian Mukeba, on 11 October 2012 - 02:05 AM, said:

if its easy to u why dnt you hlp me solve my issue..we all not genius and i am stil learning


sorry Mr. Christian if i make you angry :D, it was joking, you seems so stress with this stuff, so i try to make you laugh, once again sorry. :D, for you know, i'm new too with this (about 2 months).

copy paste this :

Private Sub Unit_OrderBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Unit_OrderBindingNavigatorSaveItem.Click
        Dim conn As New SqlClient.SqlConnection(My.Settings.NewEvapcoDBConnectionString.ToString)
        Try
            conn.Open()
            BindingNavigatorAddNewItem.Visible = True
            If UnitSerialNumberTextBox.Text = "" Then
                MessageBox.Show("Enter the Unit Serial Number ", "Unit Order")
            Else
                Me.Validate()
                Me.Unit_OrderBindingSource.EndEdit()
                Me.Unit_OrderTableAdapter.Update(Me.UnitOrderDataSet.Unit_Order)
                For i As Integer = 0 To Me.AccessoriesListBox.Items.Count - 1
                    Dim sqlcmd As String = "INSERT INTO Unit Order (Accessories) values (@Basin)"
                    Dim cmd As New SqlClient.SqlCommand(sqlcmd, conn)
                    cmd.Parameters.AddWithValue("@Basin", Me.AccessoriesListBox.Items(i))
                    cmd.ExecuteNonQuery()
                Next
                MessageBox.Show("Update Successful", "Unit Order")
                Me.BindingNavigatorAddNewItem.Visible = True
                Me.BindingNavigatorDeleteItem.Visible = True
            End If
            conn.Close()
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub



and tell us if there is an error.

This post has been edited by artemix22: 11 October 2012 - 02:21 AM

Was This Post Helpful? 0
  • +
  • -

#22 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 02:35 AM

its ok Artemix22 i wasnt angry...
i think i found sumthing its looks like as my table name is unit Order and giving me the error incorrect syntax near order means as order is a reserved word in SQL server i have to put Unit order into bracket something like [unit Order] its seems to work but now its adding the items in a new row what the f**** is dat...sorry
Was This Post Helpful? 0
  • +
  • -

#23 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 02:55 AM

here is what i do, i try it, and it works fine :

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Using conn As New SqlConnection("your connection string goes here")
                conn.Open()
                For i As Integer = 0 To ListBox1.Items.Count - 1
                    Dim command As New SqlCommand("insert into user_data (user_password) values (@user_password)", conn)
                    command.Parameters.AddWithValue("@user_password", ListBox1.Items(i).ToString)
                    command.ExecuteNonQuery()
                Next
                MessageBox.Show("It's Saved!")
                conn.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class



you'll need to modify it to your purpose.

next time, if you want create table in database, if it has two words or more, use "_" between those words.
example : Unit_Order

This post has been edited by artemix22: 11 October 2012 - 03:01 AM

Was This Post Helpful? 0
  • +
  • -

#24 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: how can i save multiple listbox items into a database table

Posted 11 October 2012 - 03:32 AM

ok i'l do dat then il let you knw whether its working or not....i appreciate your help!!!
Was This Post Helpful? 0
  • +
  • -

#25 manikandan75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: how can i save multiple listbox items into a database table

Posted 18 October 2012 - 06:46 PM

how to create bindingnavigatorsaveitem frm vb
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2