I do the simple project using VB.net 2008 to link with MS Access 2007. I have two textbox which is data for ID and Fullname.
txtID: ID and txtFullname : Fullname and Button1 : Search
In MS Access My data for ID : 1,2 & 3 and My data for Fullname : John, Peter & Mike
The problem I facing now is only when I key-in data '1' in txtID, output was appear in txtFullname. But when I type '2' in txtID, the output is not appear in txtFullname.
Only ID 1 I can call. BUt another ID can't.
PLease Help me.
This is my code :
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\sample.accdb"
con.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SQL As String = "SELECT * FROM phonebookTable WHERE ID= ?"
If con.State = ConnectionState.Closed Then
con.Open()
End If
Using cmd As New OleDbCommand(SQL, con)
cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(txtID.Text))
Dim rdr As OleDbDataReader = cmd.ExecuteReader
If rdr.HasRows Then
rdr.Read()
' use the actual DB column name
txtFullname.Text = rdr.Item("Fullname").ToString
Else
txtFullname.Text = "No records"
End If
End Using
End Sub
End Class