Solved

error during for loop

Posted on 2006-11-20
10
189 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 62

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 62

Expert Comment

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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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