如何将我在listview中选择的项目放入我的数据库?

时间:2017-10-08 00:50:04

标签: vb.net

See pic 刚接触vb.net。无论如何,我不知道如何将我在数据库中选择的产品和价格的价值放在列表视图中。我试过了 将txtValue调暗为String txtValue = ListView1.FocusedItem.SubItems(0).text。获取列的值。 在我提供的图片中,如果我输入客户名,我在listview1中选择她的订单,它将保存在我的数据库中。它会显示我的listview2。只是无视地址 更新我认为此代码有效,但仍然显示错误消息。Error message see pic

Imports System.Data.SqlClient
Imports System.IO

Public Class Form1
    Dim con As SqlConnection = New SqlConnection("server=.\SQL;database=try;Trusted_Connection=TRUE")
    Dim cmd As SqlCommand
    Dim cmd2 As SqlCommand
    Dim rdr As SqlDataReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        con.Open()
        con.Close()
        list()

        list2()
    End Sub
    Sub list()
        con.Open()


        cmd = New SqlCommand("SELECT * FROM ProductTable", con)
        rdr = cmd.ExecuteReader
        ListView1.Items.Clear()
        If rdr.HasRows Then
            Do While rdr.Read()
                Dim arr As String() = New String(2) {}
                Dim itm As ListViewItem

                arr(0) = rdr("productID")
                arr(1) = rdr("product")
                arr(2) = rdr("price")

                itm = New ListViewItem(arr)
                ListView1.Items.Add(itm)


            Loop

        End If

        con.Close()
    End Sub


    Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click

        modifyrecord("Insert into ProductOrder([name],[product],[price]) values ('" & txtname.Text & "','" & ListView1.SelectedItems(0).SubItems(1).Text & "'," & ListView1.SelectedItems(0).SubItems(2).Text & "")



    End Sub
    Private Sub listView1_MouseClick_1(ByVal sender As Object, ByVal e As MouseEventArgs)

    End Sub

    Sub list2()
        con.Open()


        cmd2 = New SqlCommand("SELECT * FROM ProductOrder", con)
        rdr = cmd2.ExecuteReader
        ListView2.Items.Clear()
        If rdr.HasRows Then
            Do While rdr.Read()
                Dim arr As String() = New String(3) {}
                Dim itm As ListViewItem

                arr(0) = rdr("id")
                arr(1) = rdr("name")
                arr(2) = rdr("product")
                arr(3) = rdr("price")

                itm = New ListViewItem(arr)
                ListView2.Items.Add(itm)


            Loop

        End If

        con.Close()
    End Sub
    Sub modifyrecord(ByVal sql)

        If txtname.Text = "" Or ListView1.SelectedItems(0).SubItems(1).Text = "" Or IsNumeric(ListView1.SelectedItems(0).SubItems(2).Text) = False Then


        Else
            con.Open()
            cmd = New SqlCommand(sql, con)
            cmd.ExecuteNonQuery()
            con.Close()
            list()

        End If

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

试试这个

<form action="{{ url_for('success') }}" method="post">

从列表视图中获取产品和价格 我假设您将MultiSelect设置为false。