2014-07-18 25 views
0

我正在开发一个OOB Silverlight应用程序并使用COM工具包(http://silverlightcom.codeplex.com),该语言是Visual Basic(VB)。使用这个包,我可以使用ADO.NET将我的应用程序与我的Microsoft SQL Server数据库连接起来。它的工作原理,但是当我尝试向参数中插入一个空值时,它会抛出一个异常:0x800A0BB9(http://prntscr.com/43zwyr)。AddWithValue()不接受DBNull.Value(异常0x800A0BB9) - Silverlight - VB

我的代码是(我将发布最简单的拉伸):

Private Const SqlCreateJob As String = "insert into Cargos(Nome_carg, Desc_carg) values(?, ?)" 

    Public Shared Sub CreateJob(ByVal j As Job) 

     Dim conn As AdoConnection = DbUtil.GetConn 
     Dim cmd As AdoCommand = conn.CreateCommand 
     cmd.CommandText = SqlCreateJob 

     cmd.Parameters.AddWithValue("Nome_carg", j.Name) 
     cmd.Parameters.AddWithValue("Desc_carg", j.Desc) 

     For Each p As AdoParameter In cmd.Parameters 
      If p.Value = Nothing Then 
       p.Value = DBNull.Value 
      End If 
     Next 

     cmd.ExecuteNonQuery() 
     conn.Close() 

    End Sub 

和类职位:

Public Class Job 
    Property Code() As Integer 
    Property Name() As String 
    Property Desc() As String 
End Class 

值说明,从工作,可以为空。如果我把 cmd.Parameters.AddWithValue("Desc_carg", DBNull.Value)它仍然不起作用。我能做什么? (当说明是不是空的命令作品)

的Exception.ToString是在这里:

System.Runtime.InteropServices.COMException (0x800A0BB9): Exception from HRESULT: 0x800A0BB9 ---> MS.Internal.ComAutomation.ComAutomationObjectException: The arguments are incorrect, are out of acceptable range, or are in conflict.(Source=ADODB.Parameter) (HelpFile=C:\WINDOWS\HELP\ADO270.CHM#1240641) em MS.Internal.ComAutomation.ComAutomationNative.CheckInvokeHResult(UInt32 hr, String memberName, String exceptionSource, String exceptionDescription, String exceptionHelpFile, UInt32 exceptionHelpContext) em MS.Internal.ComAutomation.ComAutomationNative.Invoke(Boolean tryInvoke, String memberName, ComAutomationInvokeType invokeType, ComAutomationInteropValue[] rgParams, IntPtr nativePeer, ComAutomationInteropValue& returnValue) em MS.Internal.ComAutomation.ComAutomationObject.InvokeImpl(Boolean tryInvoke, String name, ComAutomationInvokeType invokeType, Object& returnValue, Object[] args) em MS.Internal.ComAutomation.ComAutomationObject.Invoke(String name, ComAutomationInvokeType invokeType, Object[] args) em System.Runtime.InteropServices.Automation.AutomationMetaObjectProvider.TrySetMember(SetMemberBinder binder, Object value) em System.Runtime.InteropServices.Automation.AutomationMetaObjectProviderBase.<.cctor>b__3(Object obj, SetMemberBinder binder, Object value) em CallSite.Target(Closure , CallSite , Object , Int32) em System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) em CallSite.Target(Closure , CallSite , Object , Int32) em ComToolkit.Data.AdoParameter.set_Type(DbType value) em ComToolkit.Data.AdoParameter..ctor(Object adoParameter, String name, Object value) em CallSite.Target(Closure , CallSite , Type , Object , String , Object) em System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3) em CallSite.Target(Closure , CallSite , Type , Object , String , Object) em ComToolkit.Data.AdoCommand.CreateParameter(String name, Object value) em ComToolkit.Data.AdoParameterCollection.AddWithValue(String name, Object value) em Corporativo.Dao.CargoDao.AddParameters(AdoCommand cmd, Cargo c) em Corporativo.Dao.CargoDao.CriarCargo(Cargo c) em Corporativo.UCCadCargos.Create()  

而且.Message:

从HRESULT异常:0x800A0BB9

+0

请复制/粘贴您的异常,而不是发布链接,这样如果链接不再有效,将来可能会帮助其他人。 –

+0

好的,没问题。 –

+0

'Nome_carg'和'Desc_carg'是否都接受空值? –

回答

1

如果值为NULL或不为NULL,则不应使用AddWithValueAddWithValue从该值推断参数的数据类型,但是如果该值为NULL,则不能推断出类型。在这种情况下,您应该使用Add,明确指定数据类型并设置Value属性。