c# - Counting a specific number of records using MySqlDataReader -
i have code counts number of records same year , date. when run application doesn't work. here code:
try { string query = "select * tblorder datetime_coded=@datetimenow"; mysqlcommand cmd = new mysqlcommand(query, con); cmd.parameters.addwithvalue("@datetimenow", convert.todatetime(datetime.now).tostring("yyyy-mm")); mysqldatareader dr = cmd.executereader(); messagebox.show("ok"); con.open(); while (dr.read()) { count++; } dr.close(); con.close(); } catch (exception) { }
first have empty catch block makes no sense
atleast have been better
catch (exception ex) { messagebox(ex.message);// know if in case failed } now problem seems be
mysqldatareader dr = cmd.executereader(); messagebox.show("ok"); con.open(); <--- opening after executing reader ! you should try putting connection in using block
using(mysqlconnection con = new mysqlconnection()) { //your stuff in here } another observation
cmd.parameters.addwithvalue("@datetimenow", convert.todatetime(datetime.now).tostring("yyyy-mm"))
datetime.now datetime no need convert again
Comments
Post a Comment