Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
:(( :(( Dear Sir,
I am in great trouble. I have created a parametercollection object and adding some parameter in the collectionobjet and the objet passing to sub procedure. mu code like below

VB
Public Function InsertMCostHeaderNew(ByVal strCon As String, ByVal ModelId As String, ByVal ModelCost As Decimal, ByVal UserId As Single) As Boolean
Dim retval As Boolean 
Dim osqlParameters As SqlParameterCollection

retval = True

osqlParameters(0) = New SqlParameter("@ModelId", SqlDbType.VarChar, 5)
osqlParameters(1) = New SqlParameter("@ModelCost", SqlDbType.Decimal, 5)
osqlParameters(2) = New SqlParameter("@LogId", SqlDbType.SmallInt, 5)
osqlParameters(3) = New SqlParameter("@MCostId", SqlDbType.VarChar, 6)
osqlParameters(0).Value = ModelId
osqlParameters(1).Value = ModelCost
osqlParameters(2).Value = UserId
osqlParameters(3).Direction = ParameterDirection.Output
Try
oAppCon.ExecuteNonQuery(strCon, "usp_InsertModelCost", CommandType.StoredProcedure, osqlParameters)
Catch ex As Exception
retval = False
End Try
Return retval
End Function


But I am facing below this error when I am generating code. Please help me

Error :
Variable ‘osqlParameters’ is used before it has been assigned a value, a null reference exception could result at run time.

thanking you

Debasis Bag
Posted
Updated 17-Feb-10 21:16pm
v2

1 solution

You are adding parameters to the collection before you have initialized the collection.

Change
Dim osqlParameters As SqlParameterCollection


To

Dim osqlParameters As New SqlParameterCollection
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900