c# - How to use while(datareader.Read()) -
the following code throws exception after shows message box in if-else statement. exception says reader can't read, because closed:
try { using (mysqlconnection conn = new mysqlconnection(config.connectionstring())) { conn.open(); mysqlcommand cmd = new mysqlcommand("select * tb_useraccounts;", conn); mysqldatareader dr = cmd.executereader(); while (dr.read()) { if (username == (dr["username"].tostring()) && password == (dr["password"].tostring())) { frmmain main = new frmmain(); main.show(); login.hide(); } else if (username == string.empty || password == string.empty) { messagebox.show("please fill blank spaces!"); } else { messagebox.show("the username , password inserted incorrect!"); } dr.close(); conn.close(); } } how can make while loop working properly?
you must close reader , connection after while loop bellow
try { using (mysqlconnection conn = new mysqlconnection(config.connectionstring())) { conn.open(); mysqlcommand cmd = new mysqlcommand("select * tb_useraccounts;", conn); mysqldatareader dr = cmd.executereader(); while (dr.read()) { if (username == (dr["username"].tostring()) && password == (dr["password"].tostring())) { frmmain main = new frmmain(); main.show(); login.hide(); } else if (username == string.empty || password == string.empty) { messagebox.show("please fill blank spaces!"); } else { messagebox.show("the username , password inserted incorrect!"); } } dr.close(); conn.close(); } wiki
Comments
Post a Comment