I am getting Specified cast is not valid.
error message when try to display date from MS Access DB on VB.NET.
The code line I am trying to display the Date is
txtEntyDate.Text = sqlRead.GetDateTime(7)
I want to display ONLY date. Is anyone free to explain to me how I should code to get this right? Also, if you guys have a link of similar question that previously answered, I will highly appreciate it.
Full code:
Try
'Setup Connection String
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\igcDatabase.accdb"
sqlCom.Connection = conn
'Open Data Connection
conn.Open()
If rbMemberID.Checked = True Then
'Query
sqlCom.CommandText = "SELECT * FROM [Members] WHERE [Member ID] = @MemberID"
'Parameter is used below to prevent SQL Injection
sqlCom.Parameters.AddWithValue("MemberID", txtSearchInput.Text)
Else
MessageBox.Show("If you are not getting the result you wanted, try again with Member ID.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Query
sqlCom.CommandText = "SELECT * FROM [Members] WHERE [Member First Name] = @FirstName"
'Parameter is used below to prevent SQL Injection
sqlCom.Parameters.AddWithValue("FirstName", txtSearchInput.Text)
End If
Dim sqlRead As OleDbDataReader = sqlCom.ExecuteReader()
sqlRead.Read()
If sqlRead.HasRows = True Then
Dim membershipType As String
If sqlRead.GetValue(4) = 1 Then
membershipType = "Deluxe"
ElseIf sqlRead.GetValue(4) = 2 Then
membershipType = "Non-Deluxe"
Else
membershipType = "Week-Day"
End If
txtMemberID.Text = sqlRead.GetValue(0)
txtFirstName.Text = sqlRead.GetString(1)
txtLastName.Text = sqlRead.GetString(2)
txtGender.Text = sqlRead.GetString(3)
txtContact.Text = sqlRead.GetValue(5)
txtEmail.Text = sqlRead.GetString(6)
txtMembershipType.Text = membershipType
txtEntyDate.Text = sqlRead.GetDateTime(7)
txtStatus.Text = sqlRead.GetString(8)
Else
MessageBox.Show("There Is no such member found in database. Please Try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtMemberID.Clear()
txtFirstName.Clear()
txtLastName.Clear()
txtGender.Clear()
txtContact.Clear()
txtEmail.Clear()
txtMembershipType.Clear()
txtStatus.Clear()
txtEntyDate.Clear()
txtSearchInput.Clear()
txtSearchInput.Select()
End If
sqlCom.Parameters.Clear()
sqlRead.Close()
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try