Solved

error during for loop

Posted on 2006-11-20
10
191 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
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!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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