vb.net - Unhandled exception of type 'System.Data.OleDb.OleDbException' occured in System.Data.dll -
i'm getting error in code, designed create records in access database, can't seem work out why.
option explicit on option strict on imports system.data.oledb 'name: customercontroller.vb 'description: class acting intermediary between customer form , customer table ' contains of crud business logic 'author: alastair mcintyre 'date: 12/04/2015 public class customercontroller public const connection_string string = "provider=microsoft.ace.oledb.12.0;data source=assignment 1.accdb" public function insertcustomer(byval htcustomer hashtable) integer dim oconnection oledbconnection = new oledbconnection(connection_string) dim inumrows integer try debug.print("connection string: " & oconnection.connectionstring) oconnection.open() dim ocommand oledbcommand = new oledbcommand ocommand.connection = oconnection ocommand.commandtext = _ "insert customer (title, gender, firstname, lastname, phone, address, email, dob) values (?, ?, ?, ?, ?, ?, ?, ?);" ocommand.parameters.add("title", oledbtype.varchar, 255) ocommand.parameters.add("gender", oledbtype.varchar, 255) ocommand.parameters.add("firstname", oledbtype.varchar, 255) ocommand.parameters.add("lastname", oledbtype.varchar, 255) ocommand.parameters.add("phone", oledbtype.integer, 11) ocommand.parameters.add("address", oledbtype.varchar, 255) ocommand.parameters.add("email", oledbtype.varchar, 255) ocommand.parameters.add("dob", oledbtype.integer, 8) ocommand.parameters("title").value = cstr(htcustomer("title")) ocommand.parameters("gender").value = cstr(htcustomer("gender")) ocommand.parameters("firstname").value = cstr(htcustomer("firstname")) ocommand.parameters("lastname").value = cstr(htcustomer("lastname")) ocommand.parameters("phone").value = cint(htcustomer("phone")) ocommand.parameters("address").value = cstr(htcustomer("address")) ocommand.parameters("email").value = cstr(htcustomer("email")) ocommand.prepare() inumrows = ocommand.executenonquery() debug.print(cstr(inumrows)) debug.print("the record insterted") 'catch ex exception 'debug.print("error: " & ex.message) 'msgbox("an error occured. record wasn't inserted") oconnection.close() end try return inumrows end function end class
after commenting out error message try , debug error, i've found error occurs here "http://i.stack.imgur.com/fqgbj.png" when occurs in debub mode, application crashes, , not create record in linked database
read error message:
parameter ?_8 has no default value.
your query has 8 parameter placeholders:
ocommand.commandtext = _ "insert customer (title, gender, firstname, lastname, phone, address, email, dob) values (?, ?, ?, ?, ?, ?, ?, ?);"
then add 8 parameters:
ocommand.parameters.add("title", oledbtype.varchar, 255) ocommand.parameters.add("gender", oledbtype.varchar, 255) ocommand.parameters.add("firstname", oledbtype.varchar, 255) ocommand.parameters.add("lastname", oledbtype.varchar, 255) ocommand.parameters.add("phone", oledbtype.integer, 11) ocommand.parameters.add("address", oledbtype.varchar, 255) ocommand.parameters.add("email", oledbtype.varchar, 255) ocommand.parameters.add("dob", oledbtype.integer, 8)
then... set 7 values:
ocommand.parameters("title").value = cstr(htcustomer("title")) ocommand.parameters("gender").value = cstr(htcustomer("gender")) ocommand.parameters("firstname").value = cstr(htcustomer("firstname")) ocommand.parameters("lastname").value = cstr(htcustomer("lastname")) ocommand.parameters("phone").value = cint(htcustomer("phone")) ocommand.parameters("address").value = cstr(htcustomer("address")) ocommand.parameters("email").value = cstr(htcustomer("email"))
you need set value 8th parameter ("dob").
(side note: phone , dob shouldn't integers since they're not integer values. phone text value, dob date value.)