c# - Created a login form with a local SQL database, upon execution and after a login attempt an "ArgumentException Unhandled" error occurs -


i understand same thing has been asked before , closed due simple typo. can see don't have typos , i've tried figure out problem googling no luck.

i have created login window. main login window

i have created local sql database within visual studio (2015) store users. establish connection database have written line of code in enter button visible in main login window.

sqlconnection sqlconn = new sqlconnection(@"data source=(localdb)\mssqllocaldb;initial catalog=c: \users\nikos\desktop\nikos();\safe box\database\safeboxdb.mdf;integrated security=true;connect timeout=30;encrypt=false;trustservercertificate=true;applicationintent=readwrite;multisubnetfailover=false"); 

this path has been pasted right clicking database , selecting properties. in properties there field named connection string. that's have copied , pasted, above path in code.

this code.

        //find path sql connection         sqlconnection sqlconn = new sqlconnection(@"data source=(localdb)\mssqllocaldb;initial catalog=c:\users\nikos\desktop\nikos();\safe box\database\safeboxdb.mdf;integrated security=true;connect timeout=30;encrypt=false;trustservercertificate=true;applicationintent=readwrite;multisubnetfailover=false");         //add query actions taken once connection established, select user         string sqlquery = "select * dbo.table username = '" + txtenterusername.text.trim() + "' , password = '" + txtenterpassword.text.trim();         //add sql data adapter passing in connection , query         sqldataadapter sqldataadapter = new sqldataadapter(sqlquery, sqlconn);          //create new datatable object         datatable datatable = new datatable();         //fill sql data adapter datatable         sqldataadapter.fill(datatable);          if (datatable.rows.count == 1)         {             loginmain objformmain = new loginmain();             this.hide();             userdashboard userdash = new userdashboard();             userdash.show();         }         else         {             messagebox.show("check username , password");         } 

when run program, main login window appears it's main window, enter credentials per table in database , error press "enter" button.

argumentexception unhandled

i have checked , rechecked path can't seem working , have no idea problem is. general google searches have not helped.

due low reputation new user, cannot upload table data, have 1 row user name , password. presume these being typed correctly.

the error says keyword not supported. can't seem understand this.

edit. have reinstalled server , new path

using (sqlconnection sqlconn = new sqlconnection(@"data source=(localdb)\mssqllocaldb;attachdbfilename=c:\users\nikos\documents\safebox.mdf;integrated security=true;connect timeout=30")) 

as per new connection string. new code enter button now

private void enterbutton_click(object sender, eventargs e)     {         string sqlquery = @"select * dbo.table                  username = @user ,                        password = @pass";         using (sqlconnection sqlconn = new sqlconnection(@"data source=(localdb)\mssqllocaldb;attachdbfilename=c:\users\nikos\documents\safebox.mdf;integrated security=true;connect timeout=30"))         using (sqlcommand cmd = new sqlcommand(sqlquery, sqlconn))         {             sqlconn.open();             cmd.parameters.add("@user", sqldbtype.nvarchar).value = txtenterusername.text.trim();             cmd.parameters.add("@pass", sqldbtype.nvarchar).value = txtenterpassword.text.trim();              using (sqldatareader reader = cmd.executereader())             {                 if (reader.hasrows)                 {                     loginmain objformmain = new loginmain();                     this.hide();                     userdashboard userdash = new userdashboard();                     userdash.show();                 }                 else                 {                     messagebox.show("check username , password");                 }             }         }     } 

the new error have {"incorrect syntax near keyword 'table'."} , error points line.

using (sqldatareader reader = cmd.executereader()) 

there many errors in code.

the first 1 space between c: drive letter , remaining path wrong , should removed. adding semicolon in middle of connection string part of path confuses connectionstring parser uses semicolon separator between keys , values. origin of error message because after nikos(); semicolon parser ends discover of path , tries make sense of \safe box.... key parse.
should remove disk path , adjust connectionstring

sqlconnection sqlconn = new sqlconnection(@"data source=(localdb)\mssqllocaldb;       initial catalog=c:\users\nikos\desktop\nikos\safe box\database\safeboxdb.mdf;        integrated security=true;        connect timeout=30;        encrypt=false;        trustservercertificate=true;        applicationintent=readwrite;        multisubnetfailover=false"); 

now problems in code worse

string sqlquery = @"select * [table]                      username = @user ,                            password = @pass"; using(sqlconnection sqlconn = new sqlconnection(....)) using(sqlcommand cmd = new sqlcommand(sqlquery, sqlconn)) {      sqlconn.open();      cmd.parameters.add("@user", sqldbtype.nvarchar).value = txtenterusername.text.trim();      cmd.parameters.add("@pass", sqldbtype.nvarchar).value = txtenterpassword.text.trim();       using(sqldatareader reader = cmd.executereader())      {          if(reader.hasrows)          {               loginmain objformmain = new loginmain();               this.hide();               userdashboard userdash = new userdashboard();               userdash.show();          }          else          {               messagebox.show("check username , password");          }     } } 

first of all, don't need complex sqldataadapter if want check if user exists or not. simple sqlcommand sqldatareader fine.
second, disposable objects should go inside using statement sure when have finished use them, destroyed in case of exceptions.

finally, parameters way go when need pass values database. failing use them lead sql injection attacks or unexpected syntax errors when strings contains single quotes.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -