คือว่าจะทำปุ่มค้นหาครับแต่พอกรอกตัวเลข code ให้มันค้นหามันมาติดตรง txtCode.Text = dr.Item("Code").ToString และอยากให้มันมี MessageBoxขึ้นถ้ามันหาข้อมูลไม่เจอ
Public Function ExecuteReader(ByVal prmsql As String) As OleDbDataReader
Dim dr As OleDbDataReader
Dim comm As New OleDbCommand
connectdata()
With comm
.CommandType = CommandType.Text
.CommandText = prmsql
.Connection = conn
dr = .ExecuteReader()
Return dr
End With
End Function
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim dr As OleDbDataReader
Dim code As String = InputBox("กรุณากรอกรหัสที่ต้องการค้นหา")
Dim s As String = "select * from student where Code = ' " & code & " ' "
dr = ExecuteReader(s)
dr.Read()
txtCode.Text = dr.Item("Code").ToString มันมาติดตรงนี้ครับ
txtFname.Text = dr.Item("Fname").ToString
txtLname.Text = dr.Item("Lname").ToString
MessageBox.Show("แสดงข้อมูลแล้ว")
End Sub
End Class
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim dr As OleDbDataReader
Dim code As String = InputBox("กรุณากรอกรหัสที่ต้องการค้นหา")
Dim s As String = "select * from student where Code = ' " & code & " ' "
dr = ExecuteReader(s) if dr.HasRows Then
dr.Read()
txtCode.Text = dr.Item("Code").ToString มันมาติดตรงนี้ครับ
txtFname.Text = dr.Item("Fname").ToString
txtLname.Text = dr.Item("Lname").ToString
MessageBox.Show("แสดงข้อมูลแล้ว") else
messagebox.Show("ไม่เจอข้อมูล") end if
End Sub
End Class
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim code As String = InputBox("กรุณากรอกรหัสที่ต้องการค้นหา")
If HasData(code) Then
Dim Dr As DataRow = GetData(code).Rows(0)
txtCode.Text = Dr("Code").ToString()
txtFname.Text = Dr("Fname").ToString()
txtLname.Text = Dr("Lname").ToString()
MessageBox.Show("แสดงข้อมูลแล้ว")
Else
MessageBox.Show("ไม่พบข้อมูล")
End If
End Sub
Private Function GetData(codeString As String) As DataTable
Dim connString As String = "connection string."
Dim conn As New OleDbConnection(connString)
Dim commString As String = "Select * From [student] Where [Code]=@code"
Dim comm As New OleDbCommand(commString, conn)
comm.Parameters.AddWithValue("@code", codeString)
Dim Dt As New DataTable()
Try
Dim dataAdapter As New OleDbDataAdapter()
dataAdapter.SelectCommand = comm
dataAdapter.Fill(Dt)
Catch ex As Exception
MessageBox.Show(String.Format("error: {0}", ex.Message))
Return Nothing
End Try
Return Dt
End Function
Private Function HasData(codeString As String) As Boolean
Dim connString As String = "connection string."
Dim conn As New OleDbConnection(connString)
Dim commString As String = "Select Count(*) From [student] Where [Code]=@code"
Dim comm As New OleDbCommand(commString, conn)
comm.Parameters.AddWithValue("@code", codeString)
Dim result As Integer = 0
Try
conn.Open()
result = CInt(comm.ExecuteScalar())
conn.Close()
Catch ex As Exception
MessageBox.Show(String.Format("error: {0}", ex.Message))
End Try
Return If((result > 0), True, False)
End Function
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim dr As OleDbDataReader
Dim code As String = TextBox1.Text
Dim s As String = "select * from student where Code = ' " & code & " ' "
dr = ExecuteReader(s)
if dr.HasRows Then
dr.Read()
txtCode.Text = dr.Item("Code").ToString มันมาติดตรงนี้ครับ
txtFname.Text = dr.Item("Fname").ToString
txtLname.Text = dr.Item("Lname").ToString
MessageBox.Show("แสดงข้อมูลแล้ว")
else
messagebox.Show("ไม่เจอข้อมูล")
end if
End Sub
End Class