在ParameterCollection对象中添加参数 [英] Add Parameters in ParameterCollection Object

查看:73
本文介绍了在ParameterCollection对象中添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:((:((亲爱的先生,
我很麻烦.我创建了一个parametercollection对象,并在collectionobjet和传递给子过程的对象中添加了一些参数.亩代码如下

:(( :(( 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

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



但是当我生成代码时,我正面临着这个错误.请帮帮我

错误:
在为变量'osqlParameters'赋值之前,先使用它,否则在运行时可能会导致空引用异常.

谢谢你

Debasis Bag



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

推荐答案

要在初始化集合之前向集合中添加参数.

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

Change
Dim osqlParameters As SqlParameterCollection







To

Dim osqlParameters As New SqlParameterCollection


这篇关于在ParameterCollection对象中添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆