S
Sagar
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").Value =
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
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").Value =
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