c# - show selected value from grid view row using check box asp.net -
is there wrong in code? want show value of row 8 in gridview. have code , not working out me. no errors not show value expecting see.
protected void button1_click(object sender, eventargs e) { string str = string.empty; string strname = string.empty; foreach (gridviewrow gvrow in gridview1.rows) { checkbox chk = (checkbox)gvrow.findcontrol("chkrow"); if (chk != null & chk.checked) { str += gridview1.datakeys[gvrow.rowindex].value.tostring() + ','; strname += gvrow.cells[8].text + ','; } } strname = strname.trim(",".tochararray()); lblrecord.text = "<b>credit request: </b>" + strname; }
tried debug it. not getting cell value @ all. missing here?
update!:
manged make work. masterpage cause why not working. here code:
protected void btmdisplay_click(object sender, eventargs e) { //string data = ""; foreach (gridviewrow row in gridview1.rows) { if (row.rowtype == datacontrolrowtype.datarow) { checkbox chkrow = (row.cells[0].findcontrol("chkctrl") checkbox); if (chkrow.checked) { using (sqlconnection scn = new sqlconnection("data source = 'paulo'; initial catalog=shoppingcartdb;integrated security =true")) { scn.open(); sqlcommand cmd = new sqlcommand(@"update userdata set creditrequest = cast(replace(c.creditrequest, ',', '') int) creditrequests c inner join userdata u on c.username=u.username c.username=@username", scn); cmd.parameters.add("@username", sqldbtype.nvarchar).value = session["new"]; cmd.executenonquery(); } } } } lblmsg.text = "approved";
what want happen when checkbox clicked, row(specifically row8) updated on sql database. have tried code above giving me error:
the parameterized query '(@username nvarchar(4000))update userdata set creditrequest = ca' expects parameter '@username', not supplied.
debug update:
update 2:
i tried having this
sqlcommand cmd = new sqlcommand("update userdata set creditrequest = '" + row.cells[8].text + "' username=@username", scn); cmd.parameters.add("@username", sqldbtype.nvarchar).value = row.cells[1].text;
and error conversion failed when converting varchar value '5,000
if binding gridview
on page_load
make sure have set if(!ispostback)
if(!ispostback) { //bindgridview here }
try this
cmd.parameters.addwithvalue("@username", session["new"].tostring());
update
remove single qoutes around value of creditrequest
sqlcommand cmd = new sqlcommand("update userdata set creditrequest = " + row.cells[8].text + " username=@username", scn);
Comments
Post a Comment