c# - Gridview selected row returning null -


i have gridview contains linkbutton below cod in aspx file:

<asp:templatefield>     <itemtemplate>         <asp:linkbutton id="button3" style="float: left; margin-left: 10px"             commandargument='<%# bind("id") %>'             text="cancellation" runat="server" commandname="cancell" onclick="button3_click">                     <i aria-hidden="true" class="icon-lock" title="cancellation"></i>         </asp:linkbutton>     </itemtemplate> </asp:templatefield> 

i want when user click on link button update database table when want value of cell gridview selected row face null refrence exception in line: string barcode = dgvdata.selectedrow.cells[12].text;.

 protected void button3_click(object sender, eventargs e) {     transaction tr = new transaction();     hasinreservation.entities.db.transaction dt = new transaction();     sqlconnection connection = new sqlconnection(@"data source=192.x.x.x\sql2008;initial catalog=gardeshgarikish;user id=cms;password=******;multipleactiveresultsets=true;application name=entityframework");     connection.open();     sqlcommand sqlcmd = new sqlcommand("update transactions set iscancelled = 1 barcodenumber = @value", connection);      string barcode = dgvdata.selectedrow.cells[12].text;     sqlcmd.parameters.addwithvalue("@value", barcode);     //sqlcmd.parameters.addwithvalue("@value2", dropdownlist2.selecteditem.text);     sqlcmd.executenonquery();     connection.close(); } 

dgvdata.selectedrow won't give row button clicked. need namingcontainer that. should work you:-

linkbutton button3 = (linkbutton)sender; gridviewrow selectedrow = (gridviewrow)button3.namingcontainer; string barcode = selectedrow.cells[12].text; 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -