Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My web app sometimes struggles to load and other times doesn't load at all, and this has got me wondering why? I suspect the array, and the randomization i'm using. Please programmers, i need your help, if there is some best practise i can do to help solve this problem. This is the code i use:

VB
If Not IsPostBack Then
            Me.MultiView1.ActiveViewIndex = 0
            cnn.Open()
            cmd.Connection = cnn

            Dim arbit As New Random
            Randomize()
           
            
X:          For i = 0 To a.GetUpperBound(0)
                t = arbit.Next(1, 99)

                If Array.IndexOf(a, t) = -1 Then
                    a(i) = t

                Else
                    GoTo X
                End If

            Next


            For i = 0 To 40
                cmd.CommandText = "select * from English where Serial=" & a(i)
                adp = New SqlDataAdapter(cmd.CommandText, cnn)
                adp.Fill(ds, "English")
            Next







            cnn.Close() 'connection closed


            
            dt = New DataTable("Answered")
            dt.Columns.Add("Serial", GetType(Integer))
            dt.Columns.Add("question", GetType(String))
            dt.Columns.Add("choice1", GetType(String))
            dt.Columns.Add("choice2", GetType(String))
            dt.Columns.Add("choice3", GetType(String))
            dt.Columns.Add("choice4", GetType(String))
            dt.Columns.Add("correct", GetType(String))
            dt.Columns.Add("selected", GetType(Integer))


            
            Dim r As DataRow

            For Each r In ds.Tables("English").Rows
                dr = dt.NewRow
                dr("Serial") = dt.Rows.Count + 1
                dr("question") = r.Item("question")
                dr("choice1") = r.Item("choice1")
                dr("choice2") = r.Item("choice2")
                dr("choice3") = r.Item("choice3")
                dr("choice4") = r.Item("choice4")
                dr("correct") = r.Item("correct")
                dr("selected") = -1
                dt.Rows.Add(dr)

            Next

            Session("Answered") = dt

            Call show()
Posted
Updated 1-Apr-13 14:12pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Apr-13 19:28pm    
"Does not load" what? The debugger, or something else, a page? You should not expect good performance from debugging, why? How about the performance of the application?
—SA
[no name] 1-Apr-13 19:30pm    
"GoTo X", really? Not only bad but unnecessary. Your main problem is probably creating and filling adp 40 times for no apparent reason.
OsoJames 1-Apr-13 19:44pm    
@Sergey, debugging process is slowed down, and the page doesn't load at all sometimes. The page taking to long to load doesn't make the application perform well.
@ThePhantomUpvoter, How do i make it better?
[no name] 1-Apr-13 19:55pm    
First off, rethink your design.
I have no idea what
For i = 0 To 40
cmd.CommandText = "select * from English where Serial=" & a(i)
adp = New SqlDataAdapter(cmd.CommandText, cnn)
adp.Fill(ds, "English")
Next
you think is doing. You are running a query inside a loop and throwing the results away. Do not run queries inside loops to begin with. And it does not make any sense to me what you are doing anyway. The only thing you are using it for is using the very last result set that you get.
OsoJames 1-Apr-13 20:05pm    
@ThePhantomUpvoter, thank you for your follow up, i took out the Goto X, and it was working better than before, but the only thing was that an error kept reoccurring,
'There is no row at position 38'
'There is no row at position 39'
And with the loop, i figured that if i wanted to do the query that number of times, then a loop will do

1 solution

Substituted this code below:

VB
X:          For i = 0 To a.GetUpperBound(0)
                t = arbit.Next(1, 99)

                If Array.IndexOf(a, t) = -1 Then
                    a(i) = t

                Else
                    GoTo X
                End If

            Next


for this one below:

VB
While I <= 40
                t = arbit.Next(1, 99)
                If Array.IndexOf(a, t) = -1 Then
                    a(I) = t
                    I += 1
                End If
            End While


And it works just well
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900