help..

plumage

Active member
Joined
Jul 12, 2004
Messages
38
Programming Experience
Beginner
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

 
Which line of code is the problem on?

It could be because you are trying to retrieve some numbers, in which case you cannot use the .GetString method.

You should try using the .GetValue method
VB.NET:
		 Try
			 cnn.Open()
			 'build the dataReader
			 Dim reader As OleDbDataReader = cmd.ExecuteReader
			 'read the first row
			 If reader.Read() Then
				 'set the textBoxs with appropriate data
				 TextBox1.text = reader.GetString(1)
				 Me.TextBoxFirstName.Text = reader.GetString(2)
				 Me.TextBoxPosition.Text = reader.GetString(3)
				 TextBoxCurtsey.text = reader.GetString(4)
				 TextBoxBirth.text = reader.GetValue(5)
				 textboxhire.text = reader.GetValue(6)
				 textboxaddress.text = reader.GetString(7)
				 textboxcity.text = reader.GetString(8)
				 textboxregion.text = reader.GetString(9)
				 textboxpostcode.text = reader.GetString(10)
				 textboxcountry.text = reader.GetString(11)
				 textboxhomephone.text = reader.GetString(12)
				 textboxnotes.text = reader.GetString(15)
			 Else
				 Me.TextBoxFirstName.Text=""
				 Me.TextBoxPosition.Text=""
				 Label1.Text = "No Data Found"
			 End If
			 reader.Close()
		 Finally
			 Try
				 cnn.Close()
			 Catch : End Try
		 End Try

This is how mine looks, notice the TextBoxBirth.text uses the reader.GetValue instead of reader.GetString. Try that ande let us know.

Levyuk
 
iszit possible to reset the field of the combobox..cos i wrote it this way.it wont work
Me.cboGender.ResetText().
is there other way i can clear..cos i try.me.cboGender.selecteditems.clear() also cant work..

thanks
 
no..is sumting like u refresh the whole page..cos if i choose my gender as female and update the data.then for the next search, my gender combobox still have the female wording there.that y i wish to "reset" the text
 
Back
Top