c# - Microsoft Access Engine -
i'm trying add data visual studio access in c#. every time click button save data error message pops saying "microsoft database engine". have no clue problem is. pasted code below:
private void btnsave_click(object sender, eventargs e) { oledbconnection conn = new oledbconnection(); conn.connectionstring = @"provider=microsoft.ace.oledb.12.0;data source=d:\my monroe\semester 5\advanced programming\final project\windowsformsapplication1\windowsformsapplication1\final exam .accdb"; string fname = first_nametextbox.text; string lname = last_nametextbox.text; string snum = ssntextbox.text; string city = citytextbox.text; string state = statetextbox.text; string telnum = telephone__textbox.text; oledbcommand cmd = new oledbcommand("insert customers(first name, last name, ssn,city,state,telephone# )" + " values(@fname,@lname,@snum,@city,@state,@telnum)", connect); cmd.connection = conn; conn.open(); if (conn.state == connectionstate.open) { cmd.parameters.add("@fname", oledbtype.char, 20).value = fname; cmd.parameters.add("@lname", oledbtype.char, 20).value = lname; cmd.parameters.add("@snum", oledbtype.numeric, 20).value = snum; cmd.parameters.add("@city", oledbtype.char, 20).value = city; cmd.parameters.add("@state", oledbtype.char, 20).value = state; cmd.parameters.add("@telnum", oledbtype.numeric, 20).value = telnum; try { cmd.executenonquery(); messagebox.show("data added"); conn.close(); } catch (oledbexception ex) { messagebox.show(ex.source); conn.close(); } } else { messagebox.show("connection failed"); } }
a few things check. firstly change catch
messagebox.show(ex.message);
this more informative!
secondly on line error thrown? thirdly, please check connection string. when attach access string of form:
@"provider=microsoft.ace.oledb.12.0;data source=dbfullpath\dbname.accdb;jet oledb:engine type=5;persist security info=false;"
if there no password or
@"provider=microsoft.ace.oledb.12.0;data source=dbfullpath\dbname.accdb;jet oledb:engine type=5;jet oledb:database password = password;"
if there one.
finally, have telephone field numeric? happens numbers start 0 or international ones +?
edit
sorry think misunderstood me. wanted do, amend catch reads (in full):
catch (oledbexception ex) { messagebox.show(ex.message); conn.close(); }
Comments
Post a Comment