473,769 Members | 5,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Command.Prepare method is throwing NullReferenceEx ception 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 [NullReferenceEx ception: Object reference not set to an
instance of an object.]
System.Data.Sql Client.SqlComma nd.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
NullReferenceEx ception. 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
NullReferenceEx ception exception ?

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

Protected Overrides Function Build_CommandOb ject () As
SqlClient.SqlCo mmand
Dim IsChanged As Boolean = False
Dim oCmd As SqlClient.SqlCo mmand
Try


' create command object and SP name
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd = New SqlClient.SqlCo mmand()
oCmd.CommandTyp e = CommandType.Sto redProcedure
oCmd.CommandTex t = "MYPROC_DO"

' declare params
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd.Parameters .Add("@Action", SqlDBType.Small Int)
oCmd.Parameters .Add("@Record_G UID",
SqlDBType.uniqu eidentifier, 16)
oCmd.Parameters .Add("@ACCOUNT_ ID",
SqlDBType.uniqu eidentifier, 16)
oCmd.Parameters .Add("@Employee _ID", SqlDBType.int)

oCmd.Parameters .Add("@Employee _LanguagePrefer ence",
SqlDBType.char, 2)
oCmd.Parameters .Add("@Employee _Added_Date",
SqlDBType.datet ime)

oCmd.Parameters .Add("@Cloned_R eason", SqlDBType.varch ar,
100)
oCmd.Parameters .Add("@Current_ TRXN_ID",
SqlDBType.uniqu eidentifier, 16)
oCmd.Parameters .Add("@AuditLog _Application_ID ",
SqlDBType.TinyI nt)
oCmd.Parameters .Add("@User_ID" , SqlDBType.uniqu eidentifier,
16)
oCmd.Parameters .Add("@Process_ ID",
SqlDBType.uniqu eidentifier, 16)
oCmd.Parameters .Add("@DataChan ged_UTC_Stamp",
SqlDBType.datet ime)
If Me.SaveAction = Save_Type.Add

oCmd.Parameters .Add("@Record_U TC_StampOrigina l",
SqlDBType.DateT ime).Value = oDR(Fields.Reco rd_UTC_Stamp)
Else

oCmd.Parameters .Add("@Record_U TC_StampOrigina l",
SqlDBType.DateT ime).Value = oDR(Fields.Reco rd_UTC_Stamp,
DataRowVersion. Original)
End If

' set param values
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd.Parameters ("@Action").Val ue =
System.Convert. ToInt16(Me.Save Action)
oCmd.Parameters ("@Record_GUID" ).Value =
Common_Data.Pro perty_GET_Raw(o DR, Fields.Record_G UID)
oCmd.Parameters ("@Client_ID"). Value =
Common_Data.Pro perty_GET_Raw(o DR, Fields.@ACCOUNT _ID)
oCmd.Parameters ("@Employee_ID" ).Value =
Common_Data.Pro perty_GET_Raw(o DR, Fields.Employee _ID)
oCmd.Parameters ("@Employee_Lan guagePreference ").Value =
Common_Data.Pro perty_GET_Raw(o DR, Fields.Employee _LanguagePrefer ence)
oCmd.Parameters ("@Employee_Add ed_Date").Value
= Common_Data.Pro perty_GET_Raw(o DR, Fields.Employee _Added_Date)

oCmd.Parameters ("@Cloned_Reaso n").Value =
Common_Data.Pro perty_GET_Raw(o DR, Fields.Cloned_R eason)
oCmd.Parameters ("@Current_TRXN _ID").Value =
Common_Data.Pro perty_GET_Raw(o DR, Fields.Current_ Event_TRXN_ID)

oCmd.Parameters ("@AuditLog_App lication_ID").V alue =
_MyRoot.Applica tion_ID
oCmd.Parameters ("@User_ID").Va lue = _MyRoot.User_ID
oCmd.Parameters ("@Process_ID") .Value = _MyRoot.Process _ID
oCmd.Parameters ("@DataChanged_ UTC_Stamp").Val ue =
Common_Data.Pro perty_GET_Raw(o DR, Fields.DataChan ged_UTC_Stamp)
' prepare command
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
oCmd.Prepare()

Catch ex As Exception
Error_ToEventLo g_SET(Ex)
Throw New Exception("Prob lem encountered building command
object", ex)
End Try

Return oCmd

End Function

Thanks,
AS
Jun 27 '08 #1
3 2480
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 [NullReferenceEx ception: Object reference not set to an
instance of an object.]
System.Data.Sql Client.SqlComma nd.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
NullReferenceEx ception. 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
NullReferenceEx ception exception ?
I don't know why there is a NullReferenceEx ception. The Connection
property has to be set, and the connection has to be opened before
calling the Prepare method, but it should throw an
InvalidOperatio nException 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.Dat a"
StackTrace " at System.Data.Sql Client.SqlComma nd.Prepare()
at MYSYSTEM.DAL.Em ployee_DAL.Emp. Build_CommandOb ject() in D:
\Workspace_820\ VB.NET\MYSYSTEM .DAL.Employee_D AL\Emp.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.Dat a"
StackTrace " at System.Data.Sql Client.SqlComma nd.Prepare()
at MYSYSTEM.DAL.Em ployee_DAL.Emp. Build_CommandOb ject() in D:
\Workspace_820\ VB.NET\MYSYSTEM .DAL.Employee_D AL\Emp.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
1869
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 works fine. Form 2 has several controls, none of which ia a DatGrid control. Form 3 has several controls, none of which as a DataGrid. In this configuration everything compiles and runs fine. When I add a DataGrid control to Form 3 I get the...
12
5378
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 manuals for the proper techniques to prepare an sqlj program. When I went to try the sqlj command, I got this: Exception in thread "main" java.lang.NoClassDefFoundError: sqlj/tools/Sqlj
5
13558
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 spent 7 hours to solve this, yet i still am no step further. I might be missing something around the Updatecommand property of the oleDBDataAdapter.
3
6799
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 get an error "OleDbCommand.Prepare requires all parameters to have an explicitly set type" I have checked the parameters and it seems to me that I didn't forget anything....anybody can help me ?
5
1193
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 unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll Additional information: Object reference not set to an instance of an object. this coming from module method.... in module: Sub DisplayMessage(ByVal rtb...
1
4044
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 fields were truncated due to the 255 character limit of the Jet Excel driver. To overcome this limit, I decided to just generate CSV directly. This is where my trouble began. First I tried the StreamWriter class, implemented as per the "How to:
1
3588
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 needs some lib file so that it is not so elegant to ditribute. I tried a simple setup.nsis script to prepare a single file exe with works fine except that it does get the command-line.
11
14541
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 /home/patriat/sourcescr/input/lib/patriat_lib.pl line 226. # conect to database $dbh = DBI->connect( $ENV{ORACLE_SID}, $ENV{ORAUSER}, $ENV{ORAPASSWD}, 'Oracle'); ################################ { # inserting into job_audit table print PLOG &now, " : Inserting into the...
3
2192
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 defined constraint name), item_rate and item_desc. the copmmand is as follows... CODE:
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.