You should try something like the below sample code. The idea behind the code is to iterate all the classes and run the select query with the WHERE clause so you can get the data related to the class, you want to update ( and do not want to insert again). So if there is a row found against ANY class, it means you already have this record so go inside the UPDATE block otherwise go inside the INSERT block.
Here is the sample code: (Please check comments also)
For Each dr In Me.List_Class.SelectedItems
Dim strSQL3 As String = "SELECT * FROM PeriodDetails where Class = '" & dr & "' and Section='" & List_Section.Text & "' and Day = '" & MTC1.TodayDate & "' and Period = '" & cboPeriod.Text & "'"
Me.DaAp1 = New SqlDataAdapter(strSQL3, con)
Dim Dset1 As New DataSet
DaAp1.Fill(Dset1)
If Dset1.Tables.Count > 0 AndAlso Dset1.Tables(0).Rows.Count = 0 Then
cmd.CommandText = "INSERT INTO PeriodDetails (Class, Section, Day, Period) VALUES ('" & dr & "','" & List_Section.Text & "','" & MTC1.TodayDate & "','" & cboPeriod.Text & "')"
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Recored Successfully Added")
Else
For i As Integer = 0 To Dset1.Tables(0).Rows.Count - 1
cmd.CommandText = ("UPDATE PeriodDetails SET Period='" & cboPeriod.Text & "' WHERE Class='" & List_Class.Text & "' and Section='" & List_Section.Text & "' and Day='" & CDate(MTC1.SelectionStart) & "'")
Next i
con.Open()
strda = cmd.ExecuteReader()
con.Close()
MsgBox("Recored Successfully Update")
End If
Next
Hope it helps!
If you need further assistance please share with us.