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.
24 Replies - 2133 Views - Last Post: 18 October 2012 - 06:46 PM
#16
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?
#17
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'"
#18
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?
#19
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
#20
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.
#21
Re: how can i save multiple listbox items into a database table
Posted 11 October 2012 - 02:18 AM
christian 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
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
#22
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
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
#23
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 :
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
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
#24
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!!!
#25
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
|
|

New Topic/Question
Reply




MultiQuote


|