c# - Read CheckBox Values in ASP.Net from a SQL db? -
i have table of checkboxes inserted sql db '1' , '0'. however, retrieve values again load event, i'm not able them. code:
private void getauditchecklist() { sqlcommand cmd = null; string conn = configurationmanager.connectionstrings["connectionstring"].connectionstring; string querystring = @"select mount, braker, access, conn_net, log_book, pictures, floor, cb_lenght, channel auditor_checklist " + "where site_id = @siteid"; using (sqlconnection connection = new sqlconnection(conn)) { sqlcommand command = new sqlcommand(querystring, connection); connection.open(); cmd = new sqlcommand(querystring); cmd.connection = connection; cmd.parameters.add(new sqlparameter("@siteid", //the name of parameter map system.data.sqldbtype.nvarchar, //sqldbtype value 20, //the width of parameter "site_id")); //the name of column source //fill parameter value retrieved //from text field cmd.parameters["@siteid"].value = foo.site_id; sqldatareader reader = cmd.executereader(); while (reader.read()) { checkbox1.checked = (reader.getboolean(reader.getordinal("mount"))); checkbox2.checked = (reader.getboolean(reader.getordinal("braker"))); checkbox3.checked = (reader.getboolean(reader.getordinal("access"))); checkbox4.checked = (reader.getboolean(reader.getordinal("conn_net"))); checkbox5.checked = (reader.getboolean(reader.getordinal("log_book"))); checkbox6.checked = (reader.getboolean(reader.getordinal("pictures"))); checkbox8.checked = (reader.getboolean(reader.getordinal("floor"))); checkbox9.checked = (reader.getboolean(reader.getordinal("cb_lenght"))); checkbox10.checked = (reader.getboolean(reader.getordinal("channel"))); } reader.close(); } }
what missing checkmark sql db? below how insert sql:
private void saveauditchecklist() { if (auditchecklist != null) { sqlcommand cmd = null; string conn = configurationmanager.connectionstrings["connectionstring"].connectionstring; string querystring = @"insert auditor_checklist values(" + "@siteid, @mount, @braker, @access, @connnet, @logbook, @pictures, @floor, @cblenght, @channel) "; using (sqlconnection connection = new sqlconnection(conn)) { sqlcommand command = new sqlcommand(querystring, connection); connection.open(); cmd = new sqlcommand(querystring); cmd.connection = connection; cmd.parameters.add(new sqlparameter( "@siteid", //the name of parameter map system.data.sqldbtype.nvarchar, //sqldbtype value 20, //the width of parameter "site_id")); //the name of column source //fill parameter value retrieved //from text field cmd.parameters["@siteid"].value = foo.site_id; cmd.parameters.add(new sqlparameter("@mount", sqldbtype.bit)); cmd.parameters["@mount"].value = checkbox1.checked; cmd.parameters.add(new sqlparameter("@braker", sqldbtype.bit)); cmd.parameters["@braker"].value = checkbox2.checked; cmd.parameters.add(new sqlparameter("@access", sqldbtype.bit)); cmd.parameters["@access"].value = checkbox3.checked; cmd.parameters.add(new sqlparameter("@connnet", sqldbtype.bit)); cmd.parameters["@connnet"].value = checkbox4.checked; cmd.parameters.add(new sqlparameter("@logbook", sqldbtype.bit)); cmd.parameters["@logbook"].value = checkbox5.checked; cmd.parameters.add(new sqlparameter("@pictures", sqldbtype.bit)); cmd.parameters["@pictures"].value = checkbox6.checked; cmd.parameters.add(new sqlparameter("@floor", sqldbtype.bit)); cmd.parameters["@floor"].value = checkbox8.checked; cmd.parameters.add(new sqlparameter("@cblenght", sqldbtype.bit)); cmd.parameters["@cblenght"].value = checkbox9.checked; cmd.parameters.add(new sqlparameter("@channel", sqldbtype.bit)); cmd.parameters["@channel"].value = checkbox10.checked; cmd.executereader(); } } }
booleans stored 1 or 0 in sql database, datareader conversion you. instead use:
var mybool = reader.getboolean(i);
then assign value control's value property.