 |
|
พี่ครับ ผมอยากได้โค๊ด vb.net ค้นหาข้อมูล จาก mysql ให้มาแสดงบน listview อะครับ |
|
 |
|
|
 |
 |
|
ขอบคุณมากครับ เดียวไม่เข้าใจตรงไหนผมจะถามใหม่นะครับ ^^
|
 |
 |
 |
 |
Date :
2013-10-02 21:40:38 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ครับ อันนี้ มันมี แค่ เพิ่ม ลบ แก้ไข นิครับ โค๊ดที่ให้มา คือเอาไปแปลงใส่ อันไหนดีอะครับ พอดีผมมือใหม่ มากๆ เลย งงไปหมด...
|
 |
 |
 |
 |
Date :
2013-10-02 21:42:32 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้อะครับ ผมทำถูกไหม.. ข้อมูลไม่ขึ้น ที่ listview อะครับ
Code (VB.NET)
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim sqlQuery = "select * from tbl_people where id like '%@search%' "
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqltable As New DataTable
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.Parameters.Clear()
.Parameters.AddWithValue("search", TextBox1.Text)
End With
lvpeople.Items.Clear()
For i = 0 To sqltable.Rows.Count - 1
With lvpeople
.Items.Add(sqltable.Rows(i)("id"))
With .Items(.Items.Count - 1).SubItems
.Add(sqltable.Rows(i)("fname"))
.Add(sqltable.Rows(i)("lname"))
.Add(sqltable.Rows(i)("ide"))
End With
End With
Next
If TextBox1.Text = "" Then
loadpeople()
End If
If (TextBox1.Text).Length < 13 Then
Label2.Text = "ข้อมูลยังไม่ครบ 10 ตัว"
Label3.Text = "โปรดใส่รหัสนักศึกษาให้ครบถ้วน และถูกต้อง"
End If
If (TextBox1.Text).Length < 1 Then
Label2.Text = ""
Label3.Text = ""
End If
End Sub
|
ประวัติการแก้ไข 2013-10-02 22:02:15
 |
 |
 |
 |
Date :
2013-10-02 22:01:21 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ได้ทำส่วนของ sqladapter เลยครับ
น้องไปแก้ที่ฟังชัน loadpeople() ดีกว่าไมครับ ( เข้าใจว่าฟังก์ชันนี้ดึงฐานข้อมูลง listview เช่นกัน )
Code (ASP)
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If (TextBox1.Text).Length < 13 Then
Label2.Text = "ข้อมูลยังไม่ครบ 10 ตัว"
Label3.Text = "โปรดใส่รหัสนักศึกษาให้ครบถ้วน และถูกต้อง"
else If (TextBox1.Text).Length < 1 Then
Label2.Text = ""
Label3.Text = ""
else
loadpeople()
End If
End Sub
public sub loadpeople()
Dim sqlQuery = "select * from tbl_people where id like '%@search%' "
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqltable As New DataTable
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.Parameters.Clear()
.Parameters.AddWithValue("search", TextBox1.Text)
End With
With sqlAdapter
.SelectCommand =sqlCommand
.Fill(sqltable )
End With
lvpeople.Items.Clear()
For i = 0 To sqltable.Rows.Count - 1
With lvpeople
.Items.Add(sqltable.Rows(i)("id"))
With .Items(.Items.Count - 1).SubItems
.Add(sqltable.Rows(i)("fname"))
.Add(sqltable.Rows(i)("lname"))
.Add(sqltable.Rows(i)("ide"))
End With
End With
Next
end sub
|
ประวัติการแก้ไข 2013-10-03 12:29:04
 |
 |
 |
 |
Date :
2013-10-03 12:28:39 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี้โค๊ดของผมทั้งหมดอะครับ พอดี ผมทำตามพี่แล้ว จากที่ ยังไม่ใส่รหัส มันจะมีข้อมูลขึ้น กลับไม่ขึ้น ผมก็เลยงง
ตรง loadpeople นี้คือ ข้อมูลที่มาจาก mysql แสดงบน listview นะครับ พอใส่รหัสลงไป ก็ไม่มีอะไรขึ้นที่ listview เหมือนเดิม
Code (VB.NET)
Imports MySql.Data.MySqlClient
Public Class Frmsea
Public sConnection As New MySqlConnection
Private Sub Formsea_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "SERVER = localhost; USERID=root; PASSWORD=123456; DATABASE=attc;"
End If
TextBox1.MaxLength = 13
loadpeople()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim sqlQuery = "select * from tbl_people where id like '%@search%' "
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqltable As New DataTable
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.Parameters.Clear()
.Parameters.AddWithValue("search", TextBox1.Text)
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqltable)
End With
lvpeople.Items.Clear()
For i = 0 To sqltable.Rows.Count - 1
With lvpeople
.Items.Add(sqltable.Rows(i)("id"))
With .Items(.Items.Count - 1).SubItems
.Add(sqltable.Rows(i)("fname"))
.Add(sqltable.Rows(i)("lname"))
.Add(sqltable.Rows(i)("ide"))
End With
End With
Next
If TextBox1.Text = "" Then
loadpeople()
End If
If (TextBox1.Text).Length < 13 Then
Label2.Text = "ข้อมูลยังไม่ครบ 10 ตัว"
Label3.Text = "โปรดใส่รหัสนักศึกษาให้ครบถ้วน และถูกต้อง"
End If
If (TextBox1.Text).Length < 1 Then
Label2.Text = ""
Label3.Text = ""
End If
End Sub
Public Sub loadpeople()
Dim sqlQuery As String = "SELECT * FROM tbl_people "
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim TABLE As New DataTable
Dim i As Integer
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(TABLE)
End With
'แก้ไขสำหรับหน้าข้อมูล
lvpeople.Items.Clear()
For i = 0 To TABLE.Rows.Count - 1
With lvpeople
.Items.Add(TABLE.Rows(i)("id"))
With .Items(.Items.Count - 1).SubItems
.Add(table.Rows(i)("fname"))
.Add(TABLE.Rows(i)("lname"))
.Add(TABLE.Rows(i)("ide"))
End With
End With
Next
End Sub
End Class
|
 |
 |
 |
 |
Date :
2013-10-03 19:30:05 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมมีอีกโค๊ด 1 อะครับ คือโค๊ดนี้ มันค้นหา ได้นะครับพี่ แต่มันมาแค่ รหัสที่ครบของมันเท่านั้น ตรง loadpeople เหมือนข้างบน แต่ตรง textboxต่างออกไปครับ แต่ผมอยากได้ที่มันค้นหาแล้วมาทั้งหมด แบบอันไหนที่มีรหัส 1 ก็มาทั้งหมดอะครับ
Code (VB.NET)
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim sqlQuery As String = "select * FROM tbl_people WHERE id = '" & TextBox1.Text & "'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqltable As New DataTable
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqltable)
End With
lvpeople.Items.Clear()
For i = 0 To sqltable.Rows.Count - 1
With lvpeople
.Items.Add(sqltable.Rows(i)("id"))
With .Items(.Items.Count - 1).SubItems
.Add(sqltable.Rows(i)("fname"))
.Add(sqltable.Rows(i)("lname"))
.Add(sqltable.Rows(i)("ide"))
End With
End With
Next
If TextBox1.Text = "" Then
loadpeople()
End If
If (TextBox1.Text).Length < 13 Then
Label2.Text = "ข้อมูลยังไม่ครบ 10 ตัว"
Label3.Text = "โปรดใส่รหัสนักศึกษาให้ครบถ้วน และถูกต้อง"
End If
If (TextBox1.Text).Length < 1 Then
Label2.Text = ""
Label3.Text = ""
End If
End Sub
|
 |
 |
 |
 |
Date :
2013-10-03 19:39:04 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จากอันสุดท้ายน่ะครับ
ลองเปลี่ยนเป็น
Dim sqlQuery As String = "select * FROM tbl_people WHERE id like '%" & TextBox1.Text & "%'"
แทน
ปล.ผมอ่านผ่าน ๆ น่ะครับ ไม่แน่ใจว่าตรงกับที่อยากได้ไหม
|
 |
 |
 |
 |
Date :
2013-10-03 20:04:55 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับ พี่ ขอบคุณมากครับ ^^ ผมไม่รู้จะขอบคุณพวกพี่ยังไง ... ได้แล้วครับ !
|
 |
 |
 |
 |
Date :
2013-10-03 20:11:46 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอ่อ พี่ครับ ผมขอถามอีกสักเรื่อง นะครับ พอดีว่า ผมตั้งการทำปุ่มแก้ไข แล้วกด listview เพื่อแก้ไข จึงใส่
Code (VB.NET)
Private Sub lvpeople_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvpeople.SelectedIndexChanged
idue = lvpeople.SelectedItems(0).Text
End Sub
แล้ว มันจะ error ตรงนี้ตลอด เพราะถ้าเรากดออก (X) หรือไม่ได้กดแก้ไข บางทีเวลาเลือก ข้อมูลหลายรายการ มันก็จะขึ้นerror

|
 |
 |
 |
 |
Date :
2013-10-03 22:12:15 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้แบบเลือกได้ครั้งล่ะ 1
หรือว่าเป็นแบบ Multiselect ครับ
ถ้าเป็นแบบเลือกครั้งล่ะ 1 ไม่ต้องใส่ (0) ก็ได้ครับ
-------------------------
ปกติใน selectedindexchange จะวางคำสั่งที่จำเป็นต้องมีการเปลี่ยนแปลงทันที
แต่ตัวนี้แค่ดึงค่าเท่านั้น เอาออกได้ไหมครับ
แล้วไปเรียกใช้เวลาที่จำเป็น
ปล. ผมไม่ค่อยได้ใช้ listview น่ะครับ อาจผิดไปบ้างขออภัย  
|
 |
 |
 |
 |
Date :
2013-10-04 08:18:40 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือผมลองเอาออก มันจะเลือกข้อมูลในlistview มาแก้ไขไม่ได้นะครับ แล้วแบบ multiselect นี้ เลือกได้หลายๆอันป่ะครับ ??
|
 |
 |
 |
 |
Date :
2013-10-04 10:23:41 |
By :
canasso |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|