2

I am making a Attendance System in which I get Student Record from student table in a Gridview with a check box. tick the check box for students present and leave unchecked for absent. After that i submit my record to attendance system. Problem: If I Check or left unchecked it will show the Absent in Database Results after submission of attendance please check my 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 From 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") as CheckBox;
                    if (chkVar_.Checked)
                        sta = "P";

                    string StudentName = (row.FindControl("LabelName") as Label).Text;
                    string F_Name = (row.FindControl("LabelFN") as Label).Text;
                    int Number = int.Parse( (row.FindControl("LabelSr") as Label).Text);
                    //From Here to onword Database Operations. ..  
                    SqlConnection conn = new SqlConnection("Data Source=HammadMaqbool;Initial Catalog=FYP_Demo;Integrated Security=True");
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("Insert into Attendance_B values('"+Convert.ToInt32(Number)+"','"+StudentName+"','"+F_Name+"','"+sta+"')",conn);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {

        }
    }
}
7
  • SQL Injection alert - you should not concatenate together your SQL statements - use parametrized queries instead to avoid SQL injection Commented Jul 30, 2015 at 8:48
  • before asking the question debug your code first Commented Jul 30, 2015 at 8:51
  • @Hammad You forget to write else... Commented Jul 30, 2015 at 8:52
  • @too_cool added else still problem is there. . . Commented Jul 30, 2015 at 9:35
  • Marc_s ok i will. . . Use parametrized quesries. . Commented Jul 30, 2015 at 9:35

3 Answers 3

3

As You said @Thomas Krojer Answer also not solving your issue but it should do.

The resone you are getting chkVar_.Checked always false.because On Page_Load you are binding gridview again with out checking if it is a postback. Try this.

 protected void Page_Load(object sender, EventArgs e)
   {
          if (!Page.IsPostBack)
            {
                loadData();
            }
    }

It will solve your problem surely..

Sign up to request clarification or add additional context in comments.

3 Comments

thanks @too_cool, didn´t see before my lunch break, back now and +1 for you
@ThomasKrojer Welcome happy coding..you can accept my ans also :P
@ThomasKrojer Sorry man i didnot realize that it was ur comment.. little absentminded.. while replaying.. :D
3

I think you do not really write what you mean:

string sta = "";
CheckBox chkVar_ = row.FindControl("CheckAttendence") as CheckBox;
if (chkVar_.Checked)
    sta = "P";
sta = "A";

I thin you mean:

string sta = "";
CheckBox chkVar_ = row.FindControl("CheckAttendence") as CheckBox;
if (chkVar_.Checked)
{
    sta = "P";
}
else
{
    sta = "A";
}

you see the difference?

4 Comments

it is still not working. . . :( any other suggestion or solution please?
There are some Points - first: If user clicks the checkbox, did you step through your code to see, if the record is really inserted? second: if the record is inserted, how should your dataset get knowledge of this? you didn´t use the attendance table in your query.
points like? Can u solve this problem? If you can please ? Do it
Well Thomas I dnt think these points are going to solve my problem look... CheckBox record got inserted but only with absend values... not dealing with the Present one this is the problem.. . may be u r rite but i am not getting ur points exactly. . if u can please solve it for me ?
1

you just need to correct code

if (chkVar_.Checked)
{
    sta = "P";
}
else
{
    sta = "A";
}

1 Comment

correct code at page_load protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) { loadData(); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.