ช่วยดูให้ที่ครับ อยากจะเซฟจาก listview ลงDB โดยที่ where จาก id 1 id แล้ว update ที่เรา where ด้วย id
ติดตรง update
Code (VB.NET)
Private Sub save_receive()
Dim sqlEdit As String
Dim sqlSelect As String
Dim sqlEdit1 As String
Dim unit_count As String
Dim com As New SqlCommand
Dim com2 As New SqlCommand
Dim id As String
Dim dr As SqlDataReader
sqlEdit = "UPDATE orders " _
& "SET received =1" _
& " WHERE order_id ='" & txtsend_id.Text & "'"
With com
.CommandType = CommandType.Text
.CommandText = sqlEdit
.Connection = conn
.ExecuteNonQuery()
End With
Dim i As Integer
' Dim sqlInsert As String
For i = 0 To lsvReceive.Items.Count - 1
sqlSelect = "SELECT status FROM product WHERE productid ='" & lsvReceive.Items(i).SubItems(1).Text & "'"
With com2
.CommandType = CommandType.Text
.CommandText = sqlSelect
.Connection = conn
dr = .ExecuteReader
End With
dr.Read()
unit_count = dr(3).ToString
dr.Close()
sqlEdit1 = "UPDATE product " _
& " SET status = " & CStr(CInt(unit_count) - CInt(lsvReceive.Items(i).SubItems(4).Text)) _
& " WHERE productid ='" & lsvReceive.Items(i).SubItems(1).Text & "'"
With com2
.CommandType = CommandType.Text
.CommandText = sqlEdit1
.Connection = conn
.ExecuteNonQuery()
End With
Next i
End Sub
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As New SqlDataAdapter
Dim i As Integer
Dim sql As String
connetionString = "Data Source=ServerName;Initial Catalog=MyDatabase;User ID=UserName;Password=Password"
connection = New SqlConnection(connetionString)
connection.Open()
For i = 0 To dt.Rows.Count - 1
sql = "insert into product (Product_id,Product_name,Product_price) values('" & dt.Rows(i)(0) & "','" & dt.Rows(i)(1) & "','" & dt.Rows(i)(2) & "')"
adapter.InsertCommand = New SqlCommand(sql, connection)
adapter.InsertCommand.ExecuteNonQuery()
Next
connection.Close()