localdb - c#:Configuration system failed to initialize -
localdb - c#:Configuration system failed to initialize -
i'm making c# programme takes input windows form app localdb. looked through tutorials on app.config , actual calls, , looked @ other questions here , haven't found fixed it. if dont utilize try/catch breaks @ line "sqlcommand cmd = conn.createcommand();person newperson = new person(firstnamebox.text, phonebox.text, emailbox.text, lastnamebox.text);" "the configuration element not declared" haven't seen shows wrong?
try { system.data.sqlclient.sqlconnection conn = new sqlconnection(system.configuration.configurationmanager.connectionstrings["database"].connectionstring); sqlcommand cmd = conn.createcommand();person newperson = new person(firstnamebox.text, phonebox.text, emailbox.text, lastnamebox.text); cmd.commandtext = @"insert person (firstname,lastname,email,phone) values(@firstname, @lastname, @email, @phone) "; cmd.parameters.addwithvalue("@firstname", newperson.getfirstname()); cmd.parameters.addwithvalue("@lastname", newperson.getlastname()); cmd.parameters.addwithvalue("@email", newperson.getemail()); cmd.parameters.addwithvalue("@phone", newperson.getphone()); conn.open(); cmd.executenonquery(); conn.close(); if (!validateform()) { return; } } grab (exception ex) { messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.exclamation); }
app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5" /> </startup> <configsections> <sectiongroup name="applicationsettings" type="system.configuration.applicationsettingsgroup, system, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" > <section name="midterm.properties.settings" type="system.configuration.clientsettingssection, system, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" /> </sectiongroup> </configsections> <connectionstrings> <add name="database" connectionstring=" info source=(localdb)\v11.0;attachdbfilename=c:\users\matthew\documents\visual studio 2012\projects\midterm\midterm\database1.mdf;integrated security=true;pooling=false" providername="system.data.sqlclient" ></add> </connectionstrings> <connectionstrings> <add name="midterm.properties.settings.database1connectionstring" connectionstring="data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\database1.mdf;integrated security=true" providername="system.data.sqlclient" /> </connectionstrings> </configuration>
you have 2 sections configurationstrings. need have one
<connectionstrings> <add name="database" connectionstring="......."/> <add name="midterm.properties.settings.database1connectionstring" connectionstring="..... " /> </connectionstrings>
c# localdb
Comments
Post a Comment