Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Conditional object dimension
Message
From
18/02/2003 15:43:41
 
 
To
18/02/2003 13:42:38
Rex Mahel
Realm Software, Llc
Ohio, United States
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00754495
Message ID:
00754632
Views:
18
Declare the variable outside the If

OracleConnection myConnection;
OracleCommand myCommand;
if () {
}

The compiler is probably throwing the error because it can't guarantee that the object is created. It's also possible that the variable no longer exists since it was declared within the scope of the IF. I'm not completely sure if VB.Net behaves this way or not.

-Mike


>Cathi,
>
>I have the following code:
>
>
>Sub btn_chgpwd(ByVal s As Object, ByVal e As EventArgs)
>	If IsValid Then
>		myConnection = New OracleConnection(odh)
>		myConnection.Open()
>		myTrans = myConnection.BeginTransaction()
>		myCommand = myConnection.CreateCommand()
>		With myCommand
>			.CommandText = "pkg_users.UPDATE_USERS_QUESTION"
>			.CommandType = CommandType.StoredProcedure
>			.Transaction = myTrans
>			Dim P_USER_ID, P_USER_QUESTION, P_ANSWER, P_MODIFIED_BY, P_ERROR_ID, P_ERROR_MSG As OracleParameter
>			With .Parameters
>				P_USER_ID = .Add("@P_USER_ID", OracleType.VarChar, 10)
>				P_USER_ID.Value = Session("UserName")
>
>				P_USER_QUESTION = .Add("@P_USER_QUESTION", OracleType.VarChar, 120)
>				P_USER_QUESTION.Value = txtnewpassword.Text
>
>				P_ANSWER = .Add("@P_ANSWER", OracleType.VarChar, 60)
>				P_ANSWER.Value = txtrenewpassword.Text
>
>				P_MODIFIED_BY = .Add("@P_MODIFIED_BY", OracleType.VarChar, 10)
>				P_MODIFIED_BY.Value = Session("UserName")
>
>				P_ERROR_ID = .Add("@P_ERROR_ID", OracleType.Double)
>				P_ERROR_ID.Direction = ParameterDirection.Output
>
>				P_ERROR_MSG = .Add("@P_ERROR_MSG", OracleType.VarChar, 100)
>				P_ERROR_MSG.Direction = ParameterDirection.Output
>			End With
>		End With
>
>		Try
>			myCommand.ExecuteNonQuery()
>
>			If P_ERROR_ID.Value = 0 Then
>				Response.Write("before commit")
>				myTrans.Commit()
>				myConnection.Close()
>				Response.Redirect("/login/menu.aspx")
>			Else
>				myTrans.Commit()
>				myConnection.Close()
>			End If
>		Catch eex As Exception
>			Response.Write(eex.ToString())
>			myTrans.Rollback()
>			myConnection.Close()
>		End Try
>	End If
>End Sub
>
>
>
>In the TRY block above, the P_ERROR_ID.Value line give the following error:
>
> Name 'P_ERROR_ID' is not declared.
>
>and all this procedure is under the IF statement. If I move the DIM statement outside the loop, no error occurs.
>
>Is there another way to do this?
>
>
>Thanks
>
>Rex
>
>
>
>
>>Hi Rex,
>>
>>I would recommend tracing through this code in the debugger and see if ConPubs is ever created in the If statement.
>>
>>>All,
>>>
>>>I have a codebehind file that I want to be able to use either the OleDb or Oracle data providers. I have created a strProvider variable and use If statements to dimension Connections, Commands, etc for both Oracle and Oledb. After dimensioning the objects, I try to open the connection outside the if statement, since the connection object is created by that time, but the compiler says the object is undefined.
>>>
>>>
>>>Dim strProvider As String = "OracleClient"
>>>
>>>If strProvider = "OracleClient"
>>>	Dim ConPubs As New OracleConnection(odh)
>>>Else
>>>	Dim ConPubs As New OleDbConnection(odh)
>>>End If
>>>
>>>ConPubs.Open()
>>>
>>>
>>>Is there a way to set this so I can conditionally create an object and use them?
>>>
>>>Thanks
>>>
>>>Rex
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform