Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

error during for loop

Posted on 2006-11-20
10
Medium Priority
?
195 Views
Last Modified: 2010-04-23
I have the following loop
 i = 0
            For i = 0 To 7
                If (Not requests(i).Equals(System.DBNull.Value)) Then
                    request = requests(i)
                    Dim sql1 As String = " insert request_items ([req_id], [request]) select max(req_id), '" & request & "' from requests "
                    Dim myCommand1 As New SqlCommand(sql1, myConnection)
                    myCommand1.ExecuteNonQuery()
                End If
            Next

which gives me this error

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 62:             i = 0
Line 63:             For i = 0 To 7
Line 64:                 If (Not requests(i).Equals(System.DBNull.Value)) Then
Line 65:                     request = requests(i)
Line 66:                     Dim sql1 As String = " insert request_items ([req_id], [request]) select max(req_id), '" & request & "' from requests "


Source File: C:\Inetpub\loans\MemberPages\Request.aspx.vb    Line: 64

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Request.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\loans\MemberPages\Request.aspx.vb:64
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
0
Comment
Question by:deNZity
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 13

Accepted Solution

by:
newyuppie earned 800 total points
ID: 17981502
does requests() hold 8 items? remember that 0 counts, so if you are holding 7 items the counter should go from 0 to 6
0
 

Author Comment

by:deNZity
ID: 17981518
Thanks for the reply,
Yes array holds 8 itmes

Dim requests(8) As String
0
 

Author Comment

by:deNZity
ID: 17981544
the error points to this line

Line 64:                 If (Not requests(i).Equals(System.DBNull.Value)) Then
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:newyuppie
ID: 17981547
put a line break inside the loop in
request = requests(i)

and see if it ever reaches that. if it does, press F5 to run the next loop, and so on, until you can determine on which loop the error is.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17981555
maybe try changing that line
If (Not requests(i).Equals(System.DBNull.Value)) Then

to this:

If (requests(i) IsNot System.DBNull.Value) Then
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17981559
or if you are using 2003,
If (Not requests(i) Is System.DBNull.Value) Then
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 600 total points
ID: 17981561
Hi deNZity;

I do not do any Web development but the error seems to point to requests being Nothing/null. There is no object request and you are trying to access the ith element of that object.

Try it like this to see if that is the case.

        If Not request Is Nothing then
            For i = 0 To 7
                If (Not requests(i).Equals(System.DBNull.Value)) Then
                    request = requests(i)
                    Dim sql1 As String = " insert request_items ([req_id], [request]) select max(req_id), '" & request & "' from requests "
                    Dim myCommand1 As New SqlCommand(sql1, myConnection)
                    myCommand1.ExecuteNonQuery()
                End If
            Next
        Else
                MessageBox.Show("Object request is Nothing")
        End If

Fernando
0
 
LVL 14

Assisted Solution

by:jake072
jake072 earned 600 total points
ID: 17981579
Why not just:

            For i = 0 To 7
                If Not(requests(i) Is Nothing) AndAlso (Not requests(i).Equals(System.DBNull.Value)) Then
                    request = requests(i)
                    Dim sql1 As String = " insert request_items ([req_id], [request]) select max(req_id), '" & request & "' from requests "
                    Dim myCommand1 As New SqlCommand(sql1, myConnection)
                    myCommand1.ExecuteNonQuery()
                End If
            Next

That will check to unsure that requests(i) is not nothing before it tries to process the dbnull...

Jake
0
 

Author Comment

by:deNZity
ID: 17981697
Thanks for all the replies found the problem.
I hadn't assigned values to the array :(
once I did that evrything works fine.

ok if I split points for replying?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17981748
Not a problem by me. ;=)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question