View Full Version : VB.NET linkin to microsoft access


shelly121
17-02-2005, 11:41
hey ppz! i need sum help- tryn to do a mortage database for an assignment at uni, and this is wot i have at the start of the form code. i "borrowed" it and adapted it for mine, but lesson learnt-it doesnt always work that way!!!
CN = New ADODB.Connection()
CN.Provider = "MICROSOFT.JET.OLEDB.4.0" 'ACCESS CONNECTION
CN.Open("C:\Documents and Settings\Kathleen Hughes.KATHLEEN\My Documents\michelle\yr 2004-5\vb.net\ASSIGNMENT\assignmeet2\mortgage.MDB")

RS = New ADODB.Recordset()
RS.Open("select * from MORTGAGE order by mortgagecode", CN, 1, 2)
as when i use the adapter wizard it says there is a problem with the "select *" , any1 know wot iv done wrong, do i need that "RS.open("select......) etc" bit???

JoeP
17-02-2005, 11:51
I'm assuming that there is a table called MORTGAGE in the Mortgage.mdb database, and that it has a field called mortgagecode?

Joe

shelly121
17-02-2005, 11:59
Originally posted by JoePritchard
I'm assuming that there is a table called MORTGAGE in the Mortgage.mdb database, and that it has a field called mortgagecode?

Joe

ah-ha!thanks 4 that. so iv fixed that and now its come up with a second problem!!!(the part in "quote", is the part it highlights):
CN.Provider = "MICROSOFT.JET.OLEDB.4.0" 'ACCESS CONNECTION
CN.Open("C:\Documents and Settings\Kathleen Hughes.KATHLEEN\My Documents\michelle\yr 2004-5\vb.net\ASSIGNMENT\assignmeet2\mortgage.MDB")

RS = New ADODB.Recordset()
RS.Open("select * from client order by Name", CN, 1, 2)

txtFirstName.Text = RS.Fields("Name").Value
txtLastName.Text = RS.Fields("Last Name").Value
txtDOB.Text = RS.Fields("DOB").Value
txtPostcode.Text = RS.Fields("Current Postcode").Value

JoeP
17-02-2005, 12:10
What error message are you getting?

Typically that line might throw an error for a few reasons :

Control (txtFirstName) doesn't exist on the form.
Database field 'name' doesn't exist.
Control isn't visible / enabled.

Joe

shelly121
17-02-2005, 12:30
Originally posted by JoePritchard
What error message are you getting?

Typically that line might throw an error for a few reasons :

Control (txtFirstName) doesn't exist on the form.
Database field 'name' doesn't exist.
Control isn't visible / enabled.

Joe

when i play/open the form it says:
an unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Cast from type 'DBNull' to type 'String' is not valid ??
i know that txtfirstname DOES exist on the form-its a textbox. and im sure its ont he table as a column name?? but i dont understand what you mean by control isnt visible???so could that be it???

JoeP
17-02-2005, 12:36
Ahhh.....

the field is empty in the database and VB is trying to force the Null to fit in the text box property.

Try txtName.text = CStr(RS.Fields("Name").Value)

Now, not sure about this 'cos it's a while since I've played with VB.NET!

But teh problem is definitey to do with the fact that it's returning a Null value. Might be that the database is empty? No records in?

If that's the case you would get an error like this. You should be checking whether the recordset has records in it before trying to process them.

And you're lab demonstrators should be doing a better job of teaching you... :)

Joe

shelly121
17-02-2005, 12:41
Originally posted by JoePritchard
Ahhh.....

the field is empty in the database and VB is trying to force the Null to fit in the text box property.

Try txtName.text = CStr(RS.Fields("Name").Value)

Now, not sure about this 'cos it's a while since I've played with VB.NET!.......
And you're lab demonstrators should be doing a better job of teaching you... :)

Joe
that mite just work...will have to change it all- but thanks a lot - u been a gr8 help!:)

shelly121
17-02-2005, 12:54
Originally posted by shelly121
that mite just work...will have to change it all- but thanks a lot - u been a gr8 help!:)

yep its workin!! brilliant!! thanks.