错误:无法将值NULL插入列

时间:2014-03-19 19:46:51

标签: asp.net vb.net gridview ado.net

我有一个gridview绑定到sqldatasource。尝试运行更新存储过程。

我一直收到这个错误...

Cannot insert the value NULL into column 'MyFKId', table 'dbo.MyTable'; 
column does not allow nulls. INSERT fails. The statement has been 
terminated. 

我知道答案似乎很明显,我试图将空值插入到一个不允许的列中。问题是,我知道我正在为MyFKId'提供价值。在我的代码中,我设置了一个断点,看到此行运行后有一个值......

dsMyDataSource.UpdateParameters.Item("MyFKId").DefaultValue = SomeId

SomeId确实有价值。它不是空的。你觉得怎么回事? SQLDataSource的设置如下......

 <asp:SqlDataSource ID="dsMyDataSource" runat="server" ConnectionString="xxxxx"
    SelectCommand="xxxxx" SelectCommandType="StoredProcedure"  
    UpdateCommandType="Text"  
    UpdateCommand=" Execute s_MyStoredProc @MyFKId,...@UserId">
      <UpdateParameters>
             <asp:Parameter Name="MyFKId" Type="Int32" />
              ...
      </UpdateParameters>
 </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

请勿使用EXECUTE来调用存储过程。使用InsertCommand

 <asp:SqlDataSource ID="dsMyDataSource" runat="server" ConnectionString="xxxxx"
    InsertCommand="s_MyStoredProc" InsertCommandType="StoredProcedure">
      <InsertParameters>
             <asp:Parameter Name="MyFKId" Type="Int32" />
              ...
      </InsertParameters>
 </asp:SqlDataSource>

使用InsertParameters设置参数。

dsMyDataSource.InsertParameters("MyFKId").DefaultValue = SomeId

无法尝试,但希望能为你解决。

相关问题