sql - CheckBox Not Sending Checked Value to Database C# Asp -


i making attendance system in student record student table in gridview check box. tick check box students present , leave unchecked absent. after submit record attendance system. problem: if check or left unchecked show absent in database results after submission of attendance please check code.

html

 <%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="recordtablesss.webform1" %>  <!doctype html>  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title> </head> <body>     <form id="form1" runat="server">     <div>         <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" onselectedindexchanged="gridview1_selectedindexchanged">             <columns>                 <asp:templatefield>                     <itemtemplate>                         <asp:label id="labelsr" runat="server" text=<%#eval("sr_number") %>></asp:label>                     </itemtemplate>                 </asp:templatefield>                 <asp:templatefield>                     <itemtemplate>                         <asp:label id="labelname" runat="server" text=<%#eval("name") %>></asp:label>                     </itemtemplate>                 </asp:templatefield>                 <asp:templatefield>                     <itemtemplate>                         <asp:label id="labelfn" runat="server" text=<%#eval("f_name") %>></asp:label>                     </itemtemplate>                 </asp:templatefield>                 <asp:templatefield>                     <itemtemplate>                         <asp:checkbox id="checkattendence" runat="server" />                     </itemtemplate>                 </asp:templatefield>             </columns>         </asp:gridview>         <asp:button id="buttonsubmit" runat="server" text="submit" onclick="saveattendence" style="width: 61px" />     </div>     </form> </body> </html> 

code behind:

using system; using system.collections.generic; using system.data; using system.data.sqlclient; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.configuration;  namespace recordtablesss {     public partial class webform1 : system.web.ui.page     {         protected void page_load(object sender, eventargs e)         {             loaddata();         }          protected void gridview1_selectedindexchanged(object sender, eventargs e)         {          }          private void loaddata()         {             sqlconnection con = new sqlconnection("data source=hammadmaqbool;initial catalog=fyp_demo;integrated security=true");             con.open();             sqldataadapter da = new sqldataadapter("select sr_number,name,f_name registerd_student",con);             dataset ds = new dataset();             da.fill(ds);             gridview1.datasource = ds.tables[0];             gridview1.databind();         }          protected void saveattendence(object sender, eventargs e)         {             foreach (gridviewrow row in gridview1.rows)             {                 if (row.rowtype == datacontrolrowtype.datarow)                 {                     string sta = "a";                     checkbox chkvar_ = row.findcontrol("checkattendence") checkbox;                     if (chkvar_.checked)                         sta = "p";                      string studentname = (row.findcontrol("labelname") label).text;                     string f_name = (row.findcontrol("labelfn") label).text;                     int number = int.parse( (row.findcontrol("labelsr") label).text);                     //from here onword database operations. ..                       sqlconnection conn = new sqlconnection("data source=hammadmaqbool;initial catalog=fyp_demo;integrated security=true");                     conn.open();                     sqlcommand cmd = new sqlcommand("insert attendance_b values('"+convert.toint32(number)+"','"+studentname+"','"+f_name+"','"+sta+"')",conn);                     cmd.executenonquery();                 }             }         }          protected void buttonsubmit_click(object sender, eventargs e)         {          }     } } 

as said @thomas krojer answer not solving issue should do.

the resone getting chkvar_.checked false.because on page_load binding gridview again out checking if postback. try this.

 protected void page_load(object sender, eventargs e)    {           if (!page.ispostback)             {                 loaddata();             }     } 

it solve problem surely..


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -