i am doing a search page which can fill all the textfield.but if will encounter problem when i run the program,if i not wrong, problem lies with the reader part.
The problem state:
An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll
Additional information: The data value could not be converted for reasons other than sign mismatch or data overflow. For example, the data was corrupted in the data store but the row was still retrievable.
can anyone pls help me,see where i went wrong..thanks
here the code:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\Yee\My Documents\Assignment2.mdb"
Dim MyConn As New OleDbConnection(strConn)
Dim MySQL As String
MySQL = "SELECT CustomerID,CustomerName,CustomerAddress,CustomerPostCode,CustomerGender,CustomerContactNo,CustomerDOB,CustomerNRIC FROM Customer WHERE CustomerNRIC='" & txtNRIC.Text & "'"
Dim cmd As New OleDbCommand(MySQL, MyConn)
'-- CHECK FOR VALID RECORD ---
Dim ValidRecord As Boolean = True
'-- Check for missing customer nric
If txtNRIC.Text = "" Then
MsgBox("Missing customer NRIC")
ValidRecord = False
End If
Try
MyConn.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader
'read the first row
If reader.Read() Then
'set the textBoxs with appropriate data
Me.txtCustID.Text = reader.GetString(0)
Me.txtName.Text = reader.GetString(1)
Me.txtAddress.Text = reader.GetString(2)
Me.txtPostcode.Text = reader.GetString(3)
Me.cboGender.Text = reader.GetString(4)
Me.txtContactNo.Text = reader.GetString(5)
Me.txtDOB.Text = reader.GetString(6)
Me.txtIC.Text = reader.GetString(7)
Else
Me.txtCustID.ResetText()
Me.txtName.ResetText()
Me.txtAddress.ResetText()
Me.txtPostcode.ResetText()
Me.cboGender.ResetText()
Me.txtContactNo.ResetText()
Me.txtDOB.ResetText()
Me.txtIC.ResetText()
MsgBox("No Data Found")
End If
reader.Close()
Finally
Try
MyConn.Close()
Catch : End Try
End Try
End Sub
The problem state:
An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll
Additional information: The data value could not be converted for reasons other than sign mismatch or data overflow. For example, the data was corrupted in the data store but the row was still retrievable.
can anyone pls help me,see where i went wrong..thanks
here the code:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\Yee\My Documents\Assignment2.mdb"
Dim MyConn As New OleDbConnection(strConn)
Dim MySQL As String
MySQL = "SELECT CustomerID,CustomerName,CustomerAddress,CustomerPostCode,CustomerGender,CustomerContactNo,CustomerDOB,CustomerNRIC FROM Customer WHERE CustomerNRIC='" & txtNRIC.Text & "'"
Dim cmd As New OleDbCommand(MySQL, MyConn)
'-- CHECK FOR VALID RECORD ---
Dim ValidRecord As Boolean = True
'-- Check for missing customer nric
If txtNRIC.Text = "" Then
MsgBox("Missing customer NRIC")
ValidRecord = False
End If
Try
MyConn.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader
'read the first row
If reader.Read() Then
'set the textBoxs with appropriate data
Me.txtCustID.Text = reader.GetString(0)
Me.txtName.Text = reader.GetString(1)
Me.txtAddress.Text = reader.GetString(2)
Me.txtPostcode.Text = reader.GetString(3)
Me.cboGender.Text = reader.GetString(4)
Me.txtContactNo.Text = reader.GetString(5)
Me.txtDOB.Text = reader.GetString(6)
Me.txtIC.Text = reader.GetString(7)
Else
Me.txtCustID.ResetText()
Me.txtName.ResetText()
Me.txtAddress.ResetText()
Me.txtPostcode.ResetText()
Me.cboGender.ResetText()
Me.txtContactNo.ResetText()
Me.txtDOB.ResetText()
Me.txtIC.ResetText()
MsgBox("No Data Found")
End If
reader.Close()
Finally
Try
MyConn.Close()
Catch : End Try
End Try
End Sub