none
how to pass list of data (Students Daily Attendance) from GridView as new rows to sql table? RRS feed

  • Question

  •  

    Please guys, i really need ur assistance. 

    I'm doing Web-based Attendance system for school, when the teacher get list of students for a particular Subject from STUDENTS Table, all are displayed in Gridview as shown below, next the teacher will tick PRESENT for all students present. 

     

    Now to submit the list of the student MARKED as PRESENT for that Date to ATTENDANCE Table. Code to do that, pls? I have tried with the code above but its not adding the attendance. 

     

    Any assistance? Am using LinQ: DataClassesDataContext and not connectionString.

     

    Thanks for your help in advance

     

     

    Dim db = New DataClassesDataContext
            Dim dt As New Attendance
    Try
    
    For Each row As GridViewRow In GridView1.Rows
                    If (GridView1.SelectedIndex = -1) = False Then
                        Dim studentNumber As String
                        Dim FirstName As String
                        Dim lastName As String
                        Dim Present As Boolean = 0 Or 1
                        studentNumber = GridView1.Rows((GridView1).SelectedIndex).Cells(1).Text.Trim()
                        FirstName = GridView1.Rows((GridView1).SelectedIndex).Cells(2).Text.Trim()
                        lastName = GridView1.Rows((GridView1).SelectedIndex).Cells(3).Text.Trim()
                        Present = GridView1.Rows((GridView1).SelectedIndex).Cells(4).Text.Trim()
    
    
                        dt.StudentNumber.Insert(1, dt.StudentNumber)
                        dt.Subject_Code.Insert(2, dt.Subject_Code)
                        'dt.LastName.Insert(0, dt.LastName)
                        dt.Present.ToString.Insert(3, dt.Present)
                        dt.Datex.Day.ToString.Insert(5, Calendar1.SelectedDate)
                        GridView1.DataSource = row
                        GridView1.DataBind()
                        db.Attendances.InsertOnSubmit(dt)
    
                    End If
                Next
            Catch ex As Exception
            End Try
    



    • Edited by mcmish Saturday, November 5, 2011 11:47 PM
    Friday, November 4, 2011 7:08 PM

Answers

All replies

  • Hi mcmish,

    To tell you truth, I'm not good at VB.NET, according to your code, you are trying to get the records from the GridView and save them to database by LINQ to SQL.

    You just need to set the "Attendance" instance properties=> dt.StudentNumber=studentNumber, you should call: db.SubmitChanges() method at last.

    Here is a link: http://www.codeproject.com/KB/linq/LINQtoSQL_VB.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 7, 2011 6:35 AM
    Moderator
  • Alan_chen, hi.

     

    Thanks for your suggestion, i have done it using: dt.subject_CODE = selectRow.Cells(1).Text

    db.Attendance.InsertOnSubmit(dt)

     db.SubmitChanges() 

     

    but am stunned coz it's inserting 2 records at a time rather than just once.

     

    What am i missing???

     

    Thanks

    Monday, November 7, 2011 3:57 PM
  • Hi,

    I'm not sure about your logic, but you can use "SQL Profiler" to watch if the insertion T-SQL is expected.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 10, 2011 8:29 AM
    Moderator
  • Hi,

    Any updates for your problem?

    Have a  nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 16, 2011 2:16 AM
    Moderator
  • Alan_chen, hi.

     

    Sorry i haven't update you on the status after your replies, i was a bit busy with some project. the link you gave was quite helpful. I'll will make the soln available soon though my approach seems abit different and simple.

    Thanks

    regards

     

     

    Tuesday, November 22, 2011 11:56 AM