asp.net - how to map a checkbox to update the database after submit. -
i need use session datatable email value @email , base dropdownlist.
protected void page_load(object sender, eventargs e) { dropdownlist1.datasource = (datatable)session["dt"]; dropdownlist1.datavaluefield = "base"; dropdownlist1.datatextfield = "base"; dropdownlist1.databind(); } string str; protected void submit_click(object sender, eventargs e) { if (checkbox9.checked == true) { str = str + checkbox9.text + "x"; } } sqlconnection con = new sqlconnection(...); string sql = "update inquiry2 set question1 = @str email = @email , base = @base;"; con.open(); sqlcommand cmd = new sqlcommand(sql, con); cmd.parameters.addwithvalue("@email", session.dt.email); cmd.parameters.addwithvalue("@str", str); cmd.parameters.addwithvalue("@base", dropdownlist1.base); }
}
your syntax reading value out of session
wrong, cannot use session.dt.email
.
you need read datatable
out of session
, cast datatable
, this:
datatable thedatatable = null; // verify dt in session before trying if(session["dt"] != null) { thedatatable = session["dt"] datatable; } string email; // verify data table not null if(thedatatable != null) { email = datatable.rows[0]["email"].tostring(); }
now can use email
string value in sql command parameters, this:
cmd.parameters.addwithvalue("@email", email);
update:
you want wrap drop down list binding in page_load
check ispostback
, because code posted bind drop down list every time page loads, not first time, destroying whatever selection user made.
instead this:
protected void page_load(object sender, eventargs e) { if(!ispostback) { dropdownlist1.datasource = (datatable)session["dt"]; dropdownlist1.datavaluefield = "base"; dropdownlist1.datatextfield = "base"; dropdownlist1.databind(); } }
now base
value in database parameter logic should value user selected.
Comments
Post a Comment