Solution 1:
Dim i As Integer = CInt(_table.Rows(0).Item(3))
Do While i - 2 > 0
_tableBackLogs.Merge(Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'"))
i = i - 2
Loop
Solutin 2:
If i = 1 Then
Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i & "'")
ElseIf i = 2 Then
Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'")
ElseIf i = 3 Then
Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'")
'On and on....upto i=8
End If
Which one is a better solution in terms of performance and speed of execution??
UPDATE: I am storing the data into a DATATABLE….and then using it with a ListView.
Well, putting aside the concern about string concatenation, the best approach will be to get all the data needed in the minimum number of calls to the database.
You should change your code to identify which semesters are needed and then put everything together in an unique query.
So, as you said, in your comment,
(Semester=1 OR Semester=2 OR Semester=3")would be better.From your code, I assume that
semesteris a varchar field. That’s bad because, if it was a number, you could write a better query using minimum and maximum values. (Also if you can assure that semesters are in consecutive order.).(Semester >= 1 AND Semester <= 3)However, the best performance you can get out of this, is if in your datatable has the correct indexes.