Solved

error during for loop

Posted on 2006-11-20
10
192 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 200 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 150 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 150 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 63

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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