473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Command.Prepare method is throwing NullReferenceException error

Hi.

I am working on a project to migrate a web application from 1.1 to 2.0

Within in the DAL of the application, there is a call to below
function that builds a command object for later use.

Inside this function iam getting a runtime error when the command
object calls the prepare method.

oCmd.Prepare()
The error is [NullReferenceException: Object reference not set to an
instance of an object.]
System.Data.SqlClient.SqlCommand.Prepare() +85

I debugged. The oCmd object is an instance and has values in most of
the properties and also the parameters collection of the oCmd object
is set with the right parameters and values.

I noticed that the command object does not have the 'connection'
property set. That might be the reason why it throws the
NullReferenceException. But, this app has been running successfully
in .net 1.1. Is the connection property required to be set to call the
prepare method.

Can anyone suggest what might be the reason for the
NullReferenceException exception ?

Note that the .ExecuteQuery method is NOT called in this function.

Protected Overrides Function Build_CommandObject () As
SqlClient.SqlCommand
Dim IsChanged As Boolean = False
Dim oCmd As SqlClient.SqlCommand
Try


' create command object and SP name
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd = New SqlClient.SqlCommand()
oCmd.CommandType = CommandType.StoredProcedure
oCmd.CommandText = "MYPROC_DO"

' declare params
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd.Parameters.Add("@Action", SqlDBType.SmallInt)
oCmd.Parameters.Add("@Record_GUID",
SqlDBType.uniqueidentifier, 16)
oCmd.Parameters.Add("@ACCOUNT_ID",
SqlDBType.uniqueidentifier, 16)
oCmd.Parameters.Add("@Employee_ID", SqlDBType.int)

oCmd.Parameters.Add("@Employee_LanguagePreference" ,
SqlDBType.char, 2)
oCmd.Parameters.Add("@Employee_Added_Date",
SqlDBType.datetime)

oCmd.Parameters.Add("@Cloned_Reason", SqlDBType.varchar,
100)
oCmd.Parameters.Add("@Current_TRXN_ID",
SqlDBType.uniqueidentifier, 16)
oCmd.Parameters.Add("@AuditLog_Application_ID",
SqlDBType.TinyInt)
oCmd.Parameters.Add("@User_ID", SqlDBType.uniqueidentifier,
16)
oCmd.Parameters.Add("@Process_ID",
SqlDBType.uniqueidentifier, 16)
oCmd.Parameters.Add("@DataChanged_UTC_Stamp",
SqlDBType.datetime)
If Me.SaveAction = Save_Type.Add

oCmd.Parameters.Add("@Record_UTC_StampOriginal",
SqlDBType.DateTime).Value = oDR(Fields.Record_UTC_Stamp)
Else

oCmd.Parameters.Add("@Record_UTC_StampOriginal",
SqlDBType.DateTime).Value = oDR(Fields.Record_UTC_Stamp,
DataRowVersion.Original)
End If

' set param values
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd.Parameters("@Action").Value =
System.Convert.ToInt16(Me.SaveAction)
oCmd.Parameters("@Record_GUID").Value =
Common_Data.Property_GET_Raw(oDR, Fields.Record_GUID)
oCmd.Parameters("@Client_ID").Value =
Common_Data.Property_GET_Raw(oDR, Fields.@ACCOUNT_ID)
oCmd.Parameters("@Employee_ID").Value =
Common_Data.Property_GET_Raw(oDR, Fields.Employee_ID)
oCmd.Parameters("@Employee_LanguagePreference").Va lue =
Common_Data.Property_GET_Raw(oDR, Fields.Employee_LanguagePreference)
oCmd.Parameters("@Employee_Added_Date").Value
= Common_Data.Property_GET_Raw(oDR, Fields.Employee_Added_Date)

oCmd.Parameters("@Cloned_Reason").Value =
Common_Data.Property_GET_Raw(oDR, Fields.Cloned_Reason)
oCmd.Parameters("@Current_TRXN_ID").Value =
Common_Data.Property_GET_Raw(oDR, Fields.Current_Event_TRXN_ID)

oCmd.Parameters("@AuditLog_Application_ID").Value =
_MyRoot.Application_ID
oCmd.Parameters("@User_ID").Value = _MyRoot.User_ID
oCmd.Parameters("@Process_ID").Value = _MyRoot.Process_ID
oCmd.Parameters("@DataChanged_UTC_Stamp").Value =
Common_Data.Property_GET_Raw(oDR, Fields.DataChanged_UTC_Stamp)
' prepare command
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd.Prepare()

Catch ex As Exception
Error_ToEventLog_SET(Ex)
Throw New Exception("Problem encountered building command
object", ex)
End Try

Return oCmd

End Function

Thanks,
AS
Jun 27 '08 #1
3 2453
Sagar wrote:
Hi.

I am working on a project to migrate a web application from 1.1 to 2.0

Within in the DAL of the application, there is a call to below
function that builds a command object for later use.

Inside this function iam getting a runtime error when the command
object calls the prepare method.

oCmd.Prepare()
The error is [NullReferenceException: Object reference not set to an
instance of an object.]
System.Data.SqlClient.SqlCommand.Prepare() +85

I debugged. The oCmd object is an instance and has values in most of
the properties and also the parameters collection of the oCmd object
is set with the right parameters and values.

I noticed that the command object does not have the 'connection'
property set. That might be the reason why it throws the
NullReferenceException. But, this app has been running successfully
in .net 1.1. Is the connection property required to be set to call the
prepare method.

Can anyone suggest what might be the reason for the
NullReferenceException exception ?
I don't know why there is a NullReferenceException. The Connection
property has to be set, and the connection has to be opened before
calling the Prepare method, but it should throw an
InvalidOperationException if that is not the case. So, according to the
documentation the code could never have worked anyway, but the exception
thrown is not the one expected.

Try to set the Connection and open it before calling Prepare, and see if
the exception goes away.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
No, I opened a connection and set it to the command object, the code
moves a little further and throws the same error.
I dont think the problem is with setting the connection object, mainly
this code works good in other machines where framework 1.1 is there.

I picked the exception data in debug mode. Can you give me some hints
by seeing this data ?

Message = "Object reference not set to an instance of an object."

InnerException = Nothing

Source = "System.Data"
StackTrace " at System.Data.SqlClient.SqlCommand.Prepare()
at MYSYSTEM.DAL.Employee_DAL.Emp.Build_CommandObject( ) in D:
\Workspace_820\VB.NET\MYSYSTEM.DAL.Employee_DAL\Em p.vb:line 137"
Jun 27 '08 #3
Sagar wrote:
No, I opened a connection and set it to the command object, the code
moves a little further and throws the same error.
I dont think the problem is with setting the connection object, mainly
this code works good in other machines where framework 1.1 is there.

I picked the exception data in debug mode. Can you give me some hints
by seeing this data ?

Message = "Object reference not set to an instance of an object."

InnerException = Nothing

Source = "System.Data"
StackTrace " at System.Data.SqlClient.SqlCommand.Prepare()
at MYSYSTEM.DAL.Employee_DAL.Emp.Build_CommandObject( ) in D:
\Workspace_820\VB.NET\MYSYSTEM.DAL.Employee_DAL\Em p.vb:line 137"
The documentation clearly says that the connection has to be opened when
calling Prepare.

However, calling Prepare on a stored procedure may be the cause of your
problem. The documentation says:

"If CommandType is set to StoredProcedure, the call to Prepare should
succeed, although it may cause a no-op."

The solution would simply be not to call Prepare on a stored procedure,
as it doesn't server any purpose anyway.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1808
by: sarah | last post by:
Using .NET C# installed Framework 1.1 with Service Pack 1 I have a MDI application in C# that contains 3 different forms. Form 1 has several controls one of which is a DataGrid control that...
12
5294
by: Rhino | last post by:
I am having an odd problem: the sqlj command on my system doesn't work. I am running DB2 (LUW) V8 (FP8) on WinXP. I haven't done an sqlj program since Version 6 of DB2 (LUW) so I checked the...
5
13543
by: Sagaert Johan | last post by:
I need some example I have a dataset populeted through a dataadapter fill method i can add rows to my dataset , but i have an exception when calling the dataadpters update method. I already...
3
6777
by: fmarchioni | last post by:
Hi all, I'm trying to do an insert using MySQL OleDb Provider. I'd like to use the prepare() method of OleDbCommand class to precompile the statement. The problem is that when I issue prepare() I...
5
1177
by: Supra | last post by:
i had usercontrol name doColor, then i added function procedure called doColor. then i added module and called from method doColor into module. the problem is i had to traced error:... An...
1
4015
by: paul.hine | last post by:
Hello, I maintain an application that pulls data from a web service and writes it to an Excel sheet. Originally, the app used OleDb to write the Excel. Exports ran fine except that some text...
1
3570
by: manouchk | last post by:
Hi, is there a standart way to prepare a single exe with nsis that pass the command line to an exe created by py2exe on windows? py2exe allows to prepare an exe that get the command-line but...
11
14457
by: gautamga | last post by:
Hi All i have created the script which calls method from other script lib and while executing i get and error Can't call method "prepare" on an undefined value at...
3
2157
by: kbcompany | last post by:
Hi I have just started learning oracle. i have created a table with name imp_master and columns item_code with constraint not null, item_name with constraint n1 not null(n1 is user...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.