I have a problem when trying to insert data into a sql database. I hope someone can help me. Every time I click on button2 a MessageBox appeares and is saying:
Error while inserting record in table...'lines' is not a recognized built-in function name.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Using sr As New System.IO.StreamReader("C:\Users\klaasjelle\Documents\Visual Studio 2017\Projects\WindowsApp2\WindowsApp2\bin\Debug\Images.txt")
Try
con.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\klaasjelle\Documents\visual studio 2017\Projects\WindowsApp3\WindowsApp3\Niks.mdf;Integrated Security=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide) VALUES (ListViewItem(lines(0)), lvItem.SubItem.Add(lines(1)), lvItem.SubItem.Add(lines(2)))"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
End Using
End Sub
End Class
As I said. Every time I click on Button2, it is saying:
Error while inserting record on table...'lines' is not a recognized built-in function name.
Can someone help me please
As suggested here is also the code that belongs to Button1_Click
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using sr As New System.IO.StreamReader("C:\Users\klaasjelle\Documents\Visual Studio 2017\Projects\WindowsApp2\WindowsApp2\bin\Debug\Images.txt")
While Not sr.EndOfStream
Dim lines As String() = sr.ReadLine.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
Dim lvItem As New ListViewItem(lines(0))
lvItem.SubItems.Add(lines(1))
lvItem.SubItems.Add(lines(2))
ListView1.Items.Add(lvItem)
End While
End Using
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End Sub
You are passing a string literal to your INSERT statement. This string is not the ListView item but simply a string that the database engine try to parse knowning anything at all about your list view and lines array.
Instead you should use a parameterized query
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide)
VALUES (@path, @size, @side)"
cmd.Parameters.Add("@path", SqlDbType.NVarChar).Value = ListViewItem(lines(0))
cmd.Parameters.Add("@size", SqlDbType.NVarChar).Value = ListViewItem(lines(1))
cmd.Parameters.Add("@side", SqlDbType.NVarChar).Value = ListViewItem(lines(2))
....
I assume that your database fields are of type NVarChar, if not then change the SqlDbType enum to the correct value and parse the input ListViewItem to the correct datatype
I am limiting my answer to the database problem, but your code seems to be incomplete. Where is defined the lines array? How do you get the reference to the ListViewItem?
Following your edit:
The variables are defined inside the button1_click event, and thus you cannot use them inside the button2_click event. You need to read the content of you listview and extract the info that you have added in the button1_click.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
using con As New SqlConnection
using cmd As New SqlCommand
con.ConnectionString = "....."
con.Open()
cmd.Connection = con
cmd.Parameters.Add("@path", SqlDbType.NVarChar)
cmd.Parameters.Add("@size", SqlDbType.NVarChar)
cmd.Parameters.Add("@side", SqlDbType.NVarChar)
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide)
VALUES (@path, @size, @side)"
For Each item as ListViewItem in ListView1
Dim path = item.Text
Dim size = item.SubItems(1)
Dim side = item.SubItems(2)
' Now you have the variables to insert in the database
cmd.Parameters("@path").Value = path
cmd.Parameters("@size").Value = size
cmd.Parameters("@side").Value = side
cmd.ExecuteNonQuery()
Next
End Using
End Using
End Sub
Note that I changed a bit the logic of the previous example. Now I create the connection and the command in a Using block. This will save me to explicitly close and destroy these commands. Then I define the parameters outside the loop while inside the loop I change only the value. (A simple optimization with no great benefits unless you have thousands of Items in your listview) Finally I loop over every item in the listview and extract the info that you want to insert in the database from each ListViewItem
You're trying to execute VB code in SQL. Which of course won't work. Get your values in your VB code and then add them as parameters to the SQL command. Something like this:
cmd.CommandText = "INSERT INTO Data (FilePath, ImageSize, ImageSide) VALUES (@FilePath, @ImageSize, @ImageSide)"
command.Parameters.Add("@FilePath", SqlDbType.NVarChar) 'Guessing on the type here
command.Parameters("@FilePath").Value = ListViewItem(lines(0))
' Repeat for the other two parameters, using the appropriate SqlDbType values for each
cmd.ExecuteNonQuery()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.