简体   繁体   中英

change value of selected records from ms access database in datagridview in vb.net

I'm working on attendance management system project in vb.net and I'm adding leave feature in it. I have a form in which teacher can add attendance datewise to a db and a leave form where teacher can add the student roll no, course and the date to and from which he will be on leave. This leave database should be used to change the status of a child to L.

This is leave table

This will change status of all the records

This is to change individual records

The source code

Imports System.Data.OleDb

Public Class Attendance

Dim con As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Attendance.accdb")

Dim con1 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Students.accdb")

Dim con2 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\AMS\Users.accdb")

Private Sub Attendance_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    con.Open()
    Dim cmd As New OleDbCommand("Select attend_date From " + ComboBox1.SelectedValue + " where attend_date = @date1", con)
    cmd.Parameters.AddWithValue("date1", DateTimePicker1.Value.Date)
        Dim myreader As OleDbDataReader = cmd.ExecuteReader
    If myreader.Read() Then
        con.Close()
        MessageBox.Show("Date and course Inserted Before")
    Else
        con.Close()
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim cmd2 As New OleDbCommand("Insert into " + ComboBox1.SelectedValue + " (stud_roll,stud_name,course_code,attend_date,status) Values(@roll,@name,@course,@date1,@status1)", con)
            cmd2.Parameters.AddWithValue("roll", row.Cells("StudRollDataGridViewTextBoxColumn").Value)
            cmd2.Parameters.AddWithValue("name", row.Cells("StudNameDataGridViewTextBoxColumn").Value)
            cmd2.Parameters.AddWithValue("course", ComboBox1.SelectedValue)
            cmd2.Parameters.AddWithValue("date1", DateTimePicker1.Value.ToShortDateString)
            cmd2.Parameters.AddWithValue("status1", row.Cells("status").Value)
            con.Open()
            cmd2.ExecuteNonQuery()
            con.Close()
        Next
        MessageBox.Show("Records inserted successfully")
        Dim i As Integer
        Dim count As Integer
        For i = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(i).Cells(2).Value = "P" Then
                count += 1
            End If
        Next
        Label3.Text = count
    End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    con1.Open()
    Dim tblname = DirectCast(ComboBox1.SelectedItem, DataRowView).Row.Field(Of String)("course_code")
    Dim cmd3 As New OleDbCommand("Select stud_roll, stud_name From [" & tblname & "]", con1)
    Dim da As New OleDbDataAdapter
    da.SelectCommand = cmd3
    Dim dt As New DataTable
    dt.Clear()
    da.Fill(dt)
    DataGridView1.DataSource = dt
    con1.Close()
End Sub

Private Sub Panel2_Paint(sender As Object, e As PaintEventArgs) Handles Panel2.Click

End Sub

Private Sub Panel4_Click(sender As Object, e As PaintEventArgs) Handles Panel4.Click

End Sub

Private Sub Label7_Click(sender As Object, e As EventArgs) Handles Label7.Click
    completeattendace.Show()
End Sub

Private Sub Label8_Click(sender As Object, e As EventArgs) Handles Label8.Click
    GetAttendance.Show()
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged      //this is from where the staus will change in all reacords
    con.Open()
    Dim cmd As New OleDbCommand("Select roll_no from leave where #" & DateTimePicker1.Value.Date & "# between from_date and to_date", con) // the command I'm trying to develop so that the roll no of the student is selected and used to compare and change status from "P" to "L" if the date of attendance is present between from_date and to_date
    Dim i As Integer
    If ComboBox2.SelectedItem = "--All Present--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "P"
        Next
    End If
    If ComboBox2.SelectedItem = "--All Absent--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "A"
        Next
    End If
    If ComboBox2.SelectedItem = "--Holiday--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "H"
        Next
    End If
    If ComboBox2.SelectedItem = "--Sunday--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "S"
        Next
    End If
End Sub

Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
    con2.Open()
    Dim cmd As New OleDbCommand("Select course_code From courses Where semester= " + ComboBox3.SelectedItem + "", con2)
    Dim da As New OleDbDataAdapter
    da.SelectCommand = cmd
    Dim dt As New DataTable
    dt.Clear()
    da.Fill(dt)
    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = "course_code"
    ComboBox1.ValueMember = "course_code"
    con2.Close()
End Sub

Private Sub Label9_Click(sender As Object, e As EventArgs) Handles Label9.Click

End Sub

Private Sub Label10_Click(sender As Object, e As EventArgs)

End Sub

End Class

Is there any command or something through which I can check if a student roll number in database is in the course I'm adding attendance of only if the date(of datetimepicker1) on which attendance will be taken is between the leave to and from dates in database and change the status of only that student to "L" till the leave date ends. Please anyone help me, my college professor doesnt know how to do it but wants me to do it.

You doun't expose the database structure (as required in Stackoverflw for SQL questions), so my answer is shown on some generic table and field names. Also, I'm not sure I clearly understand your question.

To find all students that have roll_number from a particular course (assumes tables Courses, Students and StudentErols):

SELECT ST.LastName, ST.FirstName
FROM Couses as CRS
LEFT JOIN StudentEntrols as SE on SE.CourseID = CRS.ID
LEFT JOIN Students as ST ON ST.ID = SE.StudentID 
WHERE CRS.ID = " & Me.CourseID & ";

To check if particular student has roll_number in a particular course, see if you get any results from this modified query:

SELECT ST.LastName, ST.FirstName
FROM Couses as CRS
LEFT JOIN StudentEntrols as SE on SE.CourseID = CRS.ID
LEFT JOIN Students as ST ON ST.ID = SE.StudentID 
WHERE CRS.ID = " & Me.CourseID & "
  AND ST.ID = " & Me.StudentID & "

To create attendance of all students who are enrolled in a particular course (assumes tables Courses, Students and Attendance):

INSERT INTO Attendance as ATT (StudentID, CourseID)
SELECT ST.ID, CRS.ID
FROM Courses as CRS
LEFT JOIN StudentEntrols as SE on SE.CourseID = CRS.ID
LEFT JOIN Students as ST ON ST.ID = SE.StudentID 
WHERE CRS.ID = " & Me.CourseID & ";

You can modify that to your database structure and add parameters (which are probably overkill, considering it's just Access, run probably by a single staff).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM