StrataFrame Forum

Can not Insert Null Value When I save

http://forum.strataframe.net/Topic21749.aspx

By Terry Bottorff - 1/31/2009

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

I get the above error when I try the following code?

SwStockBO1.NewRow()

SwStockBO1.swst_pk = 1

SwStockBO1.stock = 1000

' Save New Record

If SwStockBO1.Save() <> MicroFour.StrataFrame.Data.SaveUndoResult.Success Then

MsgBox("Save New Record Failed")

End If

I'm a newbie so what am I missing? TIA.

By Terry Bottorff - 1/31/2009

PS.

The table has only 2 fields. swst_pk, and stock. Both are integer and don't allow nulls.

I also tried playing with Return Alternate on Null in the BO Mapper but could not get that working. Also, played with AllowNullValueonNewRow in the properties using both True and False and did not get that to work either. My version of SF is 1.6.6.0

By Marcia G Akins - 1/31/2009

Terry Bottorff (01/31/2009)
Cannot insert the value NULL into column 'swst_pk', table 'ProRodeo.dbo.SWStocks'; column does not allow nulls. INSERT fails.
The statement has been terminated.

I get the above error when I try the following code?

SwStockBO1.NewRow()

SwStockBO1.swst_pk = 1

SwStockBO1.stock = 1000

' Save New Record

If SwStockBO1.Save() <> MicroFour.StrataFrame.Data.SaveUndoResult.Success Then

MsgBox("Save New Record Failed")

End If

I'm a newbie so what am I missing? TIA.

Is your data in SQL Server? Is swst_pk defined as a primary key? Why don't you just set it up as an identity column and use code like this:

SwStockBO1.Add();

SwStockBO1.stock = 1000;

Try

{

   SwStockBO1.Save();

}

Catch ( Exception loErr )

{

MicroFour.StrataFrame.Messaging.MessageForm.ShowMessage("Trinity Contact Management", loErr.Message.ToString(), "00000", MessageFunction.OK, MessagingIcon.Information, MessagingSounds.None);

}

If your primary key is an identity, you will not have these problems because SQL Server automagically takes care of generating it for you.

By Terry Bottorff - 2/1/2009

Yes swst_pk is my primary key. The reason I don't have it as an identity is because the table get's rewritten 3 times a day with completely new data and I did not want the primary key to get bigger and bigger and bigger even though that probably is not a good reason. I manage it with an array of the same numbers and the only thing that chages is the stock field.

Thank you for the error code I will try it and report back. Thanks again.

By Terry Bottorff - 2/1/2009

The same error that I reported previous.

By Edhy Rijo - 2/1/2009

Hi Terry,

If you want to manage your own PK updates you have to set the following BO properties:

  • PrimaryKeyIsAutoIncremented = False
  • PrimaryKeyIsUpdatable = True

Then in the BO_SetDefaultValues() or somewhere else in your code update the PrimaryKey field value.

By Peter Jones - 2/1/2009

Hi Terry,

Just a comment re 'not wanting the PK to get bigger and bigger'. I presume you a using SQL Server in which case you can be assured it can handle the load unless you have an enormous insert rate, e.g. 1 per second for 10 years would only get your Identity column up to about the 300 million mark - 'easy as' for SQL Server.

Given that the PK is being 'reset' so regularly I can't imagine it has any interest to the user but, if it has, I would recommend adding another column for that use and simply let SQL take care of the PK.

Cheers, Peter

By Trent L. Taylor - 2/2/2009

Terry, the reason that you are getting the error is answered in Edhy's post.  You will have to set the BO properties on the BO to allow the primary key field to be updated.  This is the same approach that you would take if you were using GUIDs.
By Terry Bottorff - 2/2/2009

That was it. Thank you very very much. The help here is excellant.Smile