Link to home
Start Free TrialLog in
Avatar of deNZity
deNZity

asked on

error during for loop

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
ASKER CERTIFIED SOLUTION
Avatar of newyuppie
newyuppie
Flag of Ecuador image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deNZity
deNZity

ASKER

Thanks for the reply,
Yes array holds 8 itmes

Dim requests(8) As String
Avatar of deNZity

ASKER

the error points to this line

Line 64:                 If (Not requests(i).Equals(System.DBNull.Value)) Then
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.
maybe try changing that line
If (Not requests(i).Equals(System.DBNull.Value)) Then

to this:

If (requests(i) IsNot System.DBNull.Value) Then
or if you are using 2003,
If (Not requests(i) Is System.DBNull.Value) Then
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deNZity

ASKER

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?
Not a problem by me. ;=)