Solved

Object reference not set to an instance of an object.

Posted on 2012-04-03
12
443 Views
Last Modified: 2012-04-04
I am getting the following error from the below procedure at the high lighted line :

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

error line :     _CopyWorkflowItemID = command.ExecuteScalar().ToString()



Function:

 Function GetWorkFlowItemId(ByVal StID As String) As String

        Dim connection As SqlConnection = Nothing

        Try
            connection = New SqlConnection(strConnection)
            Dim command As New SqlCommand(Me.GetSPX("spx_StCopyForm_SELECT"), connection)
            command.CommandType = CommandType.StoredProcedure

            Dim StIDParam As SqlParameter = New SqlParameter("@StID", SqlDbType.VarChar, 50)
            StIDParam.Value = StID
            command.Parameters.Add(StIDParam)

            Dim StSetParam As SqlParameter = New SqlParameter("@StSet", SqlDbType.Int)
            StSetParam.Value = _StSet
            command.Parameters.Add(StSetParam)

            Dim workflowIDParam As SqlParameter = New SqlParameter("@WorkflowID", SqlDbType.VarChar, 50)
            workflowIDParam.Value = _WorkflowID
            command.Parameters.Add(workflowIDParam)
            connection.Open()
            _CopyWorkflowItemID = command.ExecuteScalar().ToString()
            If Not String.IsNullOrEmpty(_CopyWorkflowItemID) Then
                If _CopyWorkflowItemID <> _WorkflowItemID Then
                    Return _CopyWorkflowItemID
                End If
            End If
        Finally
            If Not connection Is Nothing Then connection.Close() : connection.Dispose()
        End Try

        Return _WorkflowItemID
    End Function

Open in new window



Please help me !
0
Comment
Question by:PeteEngineer
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37800876
i think its coused because of no rows in the return of store proc
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37800938
Instead of
           _CopyWorkflowItemID = command.ExecuteScalar().ToString()

does this work
Object obj = command.ExecuteScalar()
if not isnull(obj) then           _CopyWorkflowItemID = obj.ToString()
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 37800974
Check that spx_StCopyForm_SELECT always returns a value regardless of whether data is affected or not
0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37801033
I found no data in table ... and nor inserting any thing into this table anywhere in the application.

So can we do an error handling here .. ?

How to do that ...

can we do by checking null of that object using a if statement

if so , what we can write in the else statement to handle this ?

Please write the code by editing my previous function posted in question.
0
 
LVL 25

Assisted Solution

by:Rouchie
Rouchie earned 333 total points
ID: 37801063
What exactly is the purpose of spx_StCopyForm_SELECT ?
Can you post the code within it?  Once this is established we can look at where to place the handling.
0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37801107
its just a select command :



ALTER PROCEDURE [dbo].[spx_StDetailCopyForm_SELECT](
@StyleID uniqueidentifier,
@StyleSet int,
@WorkflowID uniqueidentifier)

AS 
BEGIN
	SELECT	 WorkFlowItemId 
	FROM	  pStDetailForm 
	WHERE	 StyleID = @StyleID AND 
			StyleSet = @StyleSet AND 
			WorkflowID = @WorkflowID
END

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37801140
>>can we do by checking null of that object using a if statement
See my first comment.
I'm not a VB specialist but basically split that line into two lines of code - so you can check if the command.ExecuteScalar is actually returning an object or null.
0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37801172
yes it is null ,

but i want to know what sepecally i need to handle ..what to put in else statement if it is not null
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 167 total points
ID: 37801310
?

if not isnull(obj) then          
  _CopyWorkflowItemID = obj.ToString()
  ... what you have at present
else
  msgbox "oops, nothing returned from the sql side of things"
end if
0
 
LVL 25

Accepted Solution

by:
Rouchie earned 333 total points
ID: 37801354
Only you can answer your question.  We can write the code, but we need to know your preference, e.g. what is the code attempting to do, what should it do if it succeeds, and what if it fails?
0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37801411
ok
0
 
LVL 15

Expert Comment

by:navneethegde
ID: 37804751
H!!

Where is _StSet  and _WorkflowID declared?


Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now