Sumathi

Sumathi

  • 1.5k
  • 235
  • 1.2k

Updating values in Grid view based on dropdown list

Apr 12 2024 8:57 AM

When I select a class from the dropdownlist, only the students from that class should appear in the gridview. Currently, all the students from all classes get displayed. When I select mark attendance(Button1) the gridview should reflect students from the class selected. Could someone help me out with the code that needs to be written under Button1 in aspx.cs page.
 

attendance.aspx: 
<body>
    <form id="form2" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" style="font-weight: 700" 
            Text="ATTENDANCE" Font-Size="Large"></asp:Label>
        <br />
        <br />
    
    </div>
    <table bgcolor="#CC66FF" border="2" class="style1">
        <tr>
            <td class="style3" bgcolor="#FFCCFF">
                Class</td>
            <td class="style4" bgcolor="#FFCCFF">
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    DataSourceID="SqlDataSource1" DataTextField="Class" DataValueField="Class" 
                    Height="66px" Width="250px">
                </asp:DropDownList>
            </td>
        </tr>
        
               <tr>
        <td>

            <strong>Date:</strong>
        </td>
        <td>
            <asp:Calendar ID="Calendar1" runat="server" 
        onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
            <asp:TextBox ID="date" runat="server" Height="30px" 
                ontextchanged="TxtBoxDate_TextChanged"></asp:TextBox>
        </td>
    </tr>
        <tr>
            <td class="style2" bgcolor="#00CCFF">
            </td>
            <td class="style5" bgcolor="#33CCFF">
                <asp:Button ID="Button1" runat="server" BackColor="#0066FF" Height="34px" 
                    Text="mark attendance" Width="144px" onclick="Button1_Click" />
            </td>
        </tr>
    </table>
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataSourceID="SqlDataSource2" ForeColor="#333333">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="RegNo" HeaderText="Reg No" 
                SortExpression="RegNo">
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="Name" HeaderText="Name" 
                SortExpression="Name">
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:TemplateField HeaderText="Mark Attendance">
                <ItemTemplate>
                    <asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="g1" 
                        Text="Present" />
                    &nbsp;&nbsp;
                    <asp:RadioButton ID="RadioButton2" runat="server" GroupName="g1" 
                        Text="Absent" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <br />
    <br />
    <asp:Button ID="Button2" runat="server" BackColor="#99CCFF" Height="41px" 
        onclick="Button2_Click" Text="Update" Width="165px" />
    <br />
    <br />
    <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:mydbConnectionString %>" 
        SelectCommand="SELECT DISTINCT [Class] FROM [student]"></asp:SqlDataSource>
    <br />
    <br />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:mydbConnectionString %>" 
        SelectCommand="SELECT DISTINCT [RegNo], [Name] FROM [student]">
    </asp:SqlDataSource>
    <br />
    <br />
    <br />
    <br />
    </form>
</body>

attendance.aspx.cs: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Faculty_attendance : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection("Data Source=DESKTOP-UIGAOQI\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True;");
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {   
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            String RegNo = row.Cells[0].Text;
            String Name = row.Cells[1].Text;
            RadioButton rbtn1 = (row.Cells[2].FindControl("RadioButton1") as RadioButton);
            RadioButton rbtn2 = (row.Cells[2].FindControl("RadioButton2") as RadioButton);
            String status1;
            if (rbtn1.Checked)
            {
                status1 = "Present";
            }
            else
            {
                status1 = "Absent";
            }
            String dateofclass1 = date.Text;
            String sclass1 = DropDownList1.SelectedItem.Text;
            saveattendance(RegNo, Name, dateofclass1, status1, sclass1);
        }
        Label4.Text = "Attendance has been saved succesfully";
    }

    private void saveattendance(String regno, String studentname, String dateofclass1, String status, String sclass)
    {
            String query = "insert into attendance(RegNo,Name, Date,Status,Class) values('" + regno + "','" + studentname + "', '" + dateofclass1 + "','" + status + "','" + sclass + "')";
            String mycon = "Data Source=DESKTOP-UIGAOQI\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";
            SqlConnection cn = new SqlConnection(mycon);
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = query;
            cmd.Connection = cn;
            cmd.ExecuteNonQuery();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        date.Text = Calendar1.SelectedDate.ToLongDateString();
    }
    protected void TxtBoxDate_TextChanged(object sender, EventArgs e)
    {
        
    }
}


Answers (5)