Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET ASM VB.NET
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:
 
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 1-Apr-13 14:24pm
Edited 1-Apr-13 15:12pm
v2
Comments
Sergey Alexandrovich Kryukov at 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
ThePhantomUpvoter at 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 at 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?
ThePhantomUpvoter at 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 at 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
ThePhantomUpvoter at 1-Apr-13 20:27pm
   
A loop is fine but querying a database is an expensive operation, especially within a loop. And even more expensive when you do not even use the result.
OsoJames at 1-Apr-13 20:11pm
   
In other words, any better code to replace this:
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
?
ThePhantomUpvoter at 1-Apr-13 20:25pm
   
For i = 0 To a.GetUpperBound(0)
t = arbit.Next(1, 99)
If Array.IndexOf(a, t) = -1 Then
a(i) = t
End If
Next
As I said the goto is completely unnecessary and there is virtually zero reason to use it.
OsoJames at 1-Apr-13 20:34pm
   
Which i now agree, but now i'm also getting this error:
'There is no row at position 29.'
That there are no rows in some positions, which i guess the GoTo X was handling. Don't know if you can suggest a way around that as well?
ThePhantomUpvoter at 1-Apr-13 20:37pm
   
No that just means that your dataset does not have any rows. Check the number of rows before trying to use the dataset. The goto would probably have nothing to do with it.
OsoJames at 1-Apr-13 20:47pm
   
I checked the table in my database, and i used the number of rows in the table to set bounds, that's why i'm confused. Because there are enough rows.
ThePhantomUpvoter at 1-Apr-13 20:54pm
   
The number of rows in your database has nothing to do with it. Your query is not returning any results back so your dataset does not have any rows. That is what the error is telling you.
OsoJames at 1-Apr-13 21:03pm
   
Ok, I am now wondering what may be wrong with my query?
ThePhantomUpvoter at 1-Apr-13 21:19pm
   
My best guess based on your code is that your database does not contain a Serial number corresponding to the random, non distinct, number in your array. You would have to compare the query with the result and your database to see what is going on.
OsoJames at 1-Apr-13 21:34pm
   
I checked as you said and it seems to be generating some random numbers more than once, could that be the problem?
ThePhantomUpvoter at 1-Apr-13 21:50pm
   
Not unless that number does not exist in your database as a serial number.
OsoJames at 2-Apr-13 16:55pm
   
Like you said i was doing some bad coding, i've resolved the "no row in position" problem by substituting the for loop with a while loop and it works just perfectly. Thanks a lot

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Substituted this code below:
 
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:
 
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
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100