รบกวนขอ code vb.net นำข้อมูลจาก listview ลงฐานข้อมูล sql
Code (VB.NET)
'วนลูปนำข้อมูลมาเก็บในตัวแปรเลยครับ ตัวอย่าง
Dim ID,ProductID,ProductName As String
For Each item As ListViewItem In Me.ListView1.Items
ID = item.SubItems.Item(0).Text
ProductID = item.SubItems.Item(1).Text
ProductName = item.SubItems.Item(2).Text
'SQL query ตามปกติ
Next
Date :
2016-07-13 11:54:40
By :
parinya-t
ปกติผมจะเก็บข้อมูลไว้ที่ datatable ละเอาไปโชว์ตาม listview gridview ก็ว่าไป
เวลา save ก็เอาจาก datatable ไปsave ง่ายดีนะครับ
Date :
2016-07-13 15:54:05
By :
Mr.T-Bag
Code (VB.NET)
***ส่วน Lable of ListView จะได้รู้ว่ามี Column อะไรบ้าง
***เขียนตั้งแต่ตอนต้นของ Code อยู่แล้ว
Private Sub lblvDetail()
With Me.lsvData
.Columns.Add("Seq.", 35, HorizontalAlignment.Center)
.Columns.Add("Item Code", 130, HorizontalAlignment.Left)
.Columns.Add("Item Description", 300, HorizontalAlignment.Left)
.Columns.Add("Comment Description", 850, HorizontalAlignment.Left)
.Columns.Add("Cust Id.", 100, HorizontalAlignment.Left)
.Columns.Add("Cust Name", 100, HorizontalAlignment.Left)
.Columns.Add("Cust Province", 100, HorizontalAlignment.Left)
.Columns.Add("Cust Region", 100, HorizontalAlignment.Left)
.Columns.Add("Cust Channel", 100, HorizontalAlignment.Left)
.Columns.Add("Cust Sale", 100, HorizontalAlignment.Left)
.Columns.Add("Date Contact", 100, HorizontalAlignment.Left)
.Columns.Add("Contact Name", 100, HorizontalAlignment.Left)
.Columns.Add("Contact Position", 100, HorizontalAlignment.Left)
.Columns.Add("Comment Type", 100, HorizontalAlignment.Left)
.View = View.Details
.GridLines = True
.FullRowSelect = True
.MultiSelect = False
.Scrollable = True
End With
End Sub
******************************************************************************
'***Start Save data to database
Dim CnCrm As New SqlConnection(ConLUFVAN)
Dim nTr As SqlTransaction
Dim sBCustomer As New StringBuilder
If MessageBox.Show(QuestionSave, SystemName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
'***Calculate Time & Datework
DateWork = Date.Now.ToString("yyyy-MM-dd hh:mm:ss")
'***************************
CnCrm.Open()
nTr = CnCrm.BeginTransaction
Try
Dim DocNo As String = RunNo2("CUSTOMERCOMMENT") & SystemType & sUserId
'*** Connection & SQL Command
sBCustomer.Remove(0, sBCustomer.Length)
sBCustomer.Append("INSERT INTO [Tsn.CustomerComment](docno,docseq,docdate,")
'***Section Customer
sBCustomer.Append("customercode,customername,customerregion,customerchannel,customerprovince,customersale,customercontact,")
'***Section Comment
sBCustomer.Append("contactdate,commenttype,productcode,productname,commentdesc,")
'***Section Document
sBCustomer.Append("docstatus,datework,userwork,customercontactposition) ")
sBCustomer.Append("VALUES (@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,")
sBCustomer.Append("@p12,@p13,@p14,@p15,@p16,@p17,@p18,@p19)")
Dim Str As String = sBCustomer.ToString
Dim cmd As New SqlCommand(Str, CnCrm, nTr)
'*** Parameters set type data
cmd.Transaction = nTr
cmd.Parameters.Clear()
cmd.Parameters.Add("@p1", SqlDbType.VarChar)
cmd.Parameters.Add("@p2", SqlDbType.Int)
cmd.Parameters.Add("@p3", SqlDbType.Date)
cmd.Parameters.Add("@p4", SqlDbType.VarChar)
cmd.Parameters.Add("@p5", SqlDbType.VarChar)
cmd.Parameters.Add("@p6", SqlDbType.VarChar)
cmd.Parameters.Add("@p7", SqlDbType.VarChar)
cmd.Parameters.Add("@p8", SqlDbType.VarChar)
cmd.Parameters.Add("@p9", SqlDbType.VarChar)
cmd.Parameters.Add("@p10", SqlDbType.VarChar)
cmd.Parameters.Add("@p11", SqlDbType.Date)
cmd.Parameters.Add("@p12", SqlDbType.VarChar)
cmd.Parameters.Add("@p13", SqlDbType.VarChar)
cmd.Parameters.Add("@p14", SqlDbType.VarChar)
cmd.Parameters.Add("@p15", SqlDbType.VarChar)
cmd.Parameters.Add("@p16", SqlDbType.VarChar)
cmd.Parameters.Add("@p17", SqlDbType.DateTime)
cmd.Parameters.Add("@p18", SqlDbType.VarChar)
cmd.Parameters.Add("@p19", SqlDbType.VarChar)
'***Section Detail on Listview
With lsvData
For i As Integer = 0 To .Items.Count - 1
Dim DocSeq As Integer = CInt(.Items(i).SubItems(0).Text)
Dim ProdCode As String = .Items(i).SubItems(1).Text
Dim ProdName As String = .Items(i).SubItems(2).Text
Dim CustomerComment As String = .Items(i).SubItems(3).Text
Dim CustomerCode As String = .Items(i).SubItems(4).Text
Dim CustomerName As String = .Items(i).SubItems(5).Text
Dim CustomerProvince As String = .Items(i).SubItems(6).Text
Dim CustomerRegion As String = .Items(i).SubItems(7).Text
Dim CustomerChannel As String = .Items(i).SubItems(8).Text
Dim CustomerSale As String = .Items(i).SubItems(9).Text
Dim ContactDate As String = .Items(i).SubItems(10).Text
Dim CustomerContact As String = .Items(i).SubItems(11).Text
Dim CustomerContactPosition As String = .Items(i).SubItems(12).Text
Dim CommentType As String = .Items(i).SubItems(13).Text
'*** Transfer data from object to parameters
cmd.Parameters("@p1").Value = DocNo
cmd.Parameters("@p2").Value = DocSeq
cmd.Parameters("@p3").Value = Date.Now.ToString("yyyy-MM-dd")
cmd.Parameters("@p4").Value = CustomerCode
cmd.Parameters("@p5").Value = CustomerName
cmd.Parameters("@p6").Value = CustomerRegion
cmd.Parameters("@p7").Value = CustomerChannel
cmd.Parameters("@p8").Value = CustomerProvince
cmd.Parameters("@p9").Value = CustomerSale
cmd.Parameters("@p10").Value = CustomerContact
cmd.Parameters("@p11").Value = ContactDate
cmd.Parameters("@p12").Value = CommentType
cmd.Parameters("@p13").Value = ProdCode
cmd.Parameters("@p14").Value = ProdName
cmd.Parameters("@p15").Value = CustomerComment
cmd.Parameters("@p16").Value = "0"
cmd.Parameters("@p17").Value = DateWork
cmd.Parameters("@p18").Value = sUserFull
cmd.Parameters("@p19").Value = CustomerContactPosition
'*** Run SQL Command
cmd.ExecuteNonQuery()
Next
End With
'***Close Connection
nTr.Commit()
CnCrm.Close()
cmd.Dispose()
sBCustomer = Nothing
'***Show messagebox
MessageBox.Show(SaveSuccess, SystemName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
nTr.Rollback()
MessageBox.Show(SaveDontSuccess & ex.Message, SystemName, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End If
***ลองดูครับ ถ้าไม่ติดต่อมาทางเมล์ หรือ line ก็ได้
[email protected]
0960506766
Date :
2016-08-05 13:59:48
By :
thaweewong
Load balance : Server 01