Could anyone help ne out twith this please? I am trying to update attendance for students. It shows a message stating, attendance has been saved successfully however the attendance isn't getting saved in the DB. Also, the names in the gridview are not getting filtered according to the class selected from the dropdownlist.
This is the code for the design page:
select class | Height="66px" Width="250px"> |
SelectCommand="SELECT DISTINCT [Class] FROM [student]">
SelectCommand="SELECT DISTINCT [RegNo], [Name] FROM [student]">
And this is the code for aspx.cs page:
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)
{
Label3.Text = "Mark Attendance for " + DateTime.Now.ToShortDateString();
}
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 = Label3.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)
{
try
{
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();
}
catch (SqlException)
{
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
}

Sam HobbsPosted Apr 6, 2024, 6:01 PM
In your
saveattendancemethod there is nothing in thecatchblock. You need to put something in thecatchblock to show an error when there is an error. Thesaveattendancemethod needs to return something indicating success or failure so that you can show a relevant message inLabel4. There is probably an error occuring in your SQL but you do not know it because thecatchblock is ignoring it.