我在学校开设销售和库存系统。我想将listview项目保存到我的数据库,这是一个MySQL sqlyog终极。
这是我的连接以及用于将listview保存到数据库的代码但我收到错误Object variable or with block variable is not set
。
Imports MySql.Data.MySqlClient
Module modconnector
Public sqlcmd As New MySqlCommand
Public sqlcon As New MySqlConnection
Public sqladapter As New MySqlDataAdapter
Public sqlreader As MySqlDataReader
Public strsql As String
Sub connect()
sqlcon.ConnectionString = ("server=localhost;user id=root;password= ;database=sais")
sqlcon.Open()
End Sub
Sub savelistview()
sqlcon.Close()
connect()
iCount = ListView1.Items.Count()
If Not ListView1.Items.Count = 0 Then
Do Until iLoop = ListView1.Items.Count
With lvitem
lvitem = ListView1.Items.Item(iLoop)
strsql = "insert into transaction(prod_code,prod_description,prod_price,order,quantity,date) values('" _
& .Items(0).Text & "','" _
& .SubItems(1).Text & "','" _
& .SubItems(2).Text & "','" _
& .SubItems(3).Text & "','" _
& .SubItems(4).Text & "','" _
& .SubItems(5).Text & "','" _
& Label26.Text & "')"
sqlcmd.CommandText = strsql
sqlcmd.Connection = sqlcon
sqladapter.SelectCommand = sqlcmd
sqlcmd.ExecuteNonQuery()
End With
lvitem = Nothing
iLoop = iLoop + 1
Loop
End If
MessageBox.Show("Record Saved!")
sqlcon.Close()
End Sub
End Module