Avatar of bobgilpin
bobgilpin
 asked on

"Column 'rowguid' does not allow nulls."

I have replicated a database to SQL Server Mobile.  The publication automatically created rowguid columns for each article.  The Merge replication synchronizes fine for updates to rows, but when I try to add a new row to the mobile database I get the error "Column 'rowguid' does not allow nulls." upon saving the row.  The rowguid column is defined with the default value "(newsequentialid())"  Thanks for any help you can give.
Microsoft SQL Server

Avatar of undefined
Last Comment
bobgilpin

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

can you show the code that performs the INSET?
Joel Coehoorn

If you have an insert statement like this:

INSERT INTO [MyTable] ([PKColumn], [OtherColumn]) VALUES (NULL, 'some data')

it will actually try to put a null in the not-nullable column.  What you want it this:

INSERT INTO [MyTable] ([OtherColumn]) VALUES ('some data')

That will use the default value and create the new id.
bobgilpin

ASKER
I didn't mention the application is VB 2005 as a VS 2005 project.  I have a simple windows form with a table adapter that contains the SQL commands for select, update and insert.  

This is the table definition (from SQL server):
CREATE TABLE [dbo].[Town](
      [TownCode] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
      [Town_cd] [int] NULL,
      [TownName] [nvarchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [TownState] [nchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [MSmerge_df_rowguid_2AF7AF2B05834382AAD04F76897941BE]  DEFAULT (newsequentialid()),
 CONSTRAINT [PK_Town] PRIMARY KEY CLUSTERED
(
      [TownCode] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

This is the VB code for the Insert command:
            Me._adapter.InsertCommand = New System.Data.SqlServerCe.SqlCeCommand
            Me._adapter.InsertCommand.Connection = Me.Connection
            Me._adapter.InsertCommand.CommandText = "INSERT INTO [Town] ([Town_cd], [TownName], [TownState]) VALUES (@p1, @p2, @p3)"
            Me._adapter.InsertCommand.CommandType = System.Data.CommandType.Text
            param = New System.Data.SqlServerCe.SqlCeParameter
            param.ParameterName = "@p1"
            param.DbType = System.Data.DbType.Int32
            param.IsNullable = true
            param.SourceColumn = "Town_cd"
            Me._adapter.InsertCommand.Parameters.Add(param)
            param = New System.Data.SqlServerCe.SqlCeParameter
            param.ParameterName = "@p2"
            param.IsNullable = true
            param.SourceColumn = "TownName"
            Me._adapter.InsertCommand.Parameters.Add(param)
            param = New System.Data.SqlServerCe.SqlCeParameter
            param.ParameterName = "@p3"
            param.DbType = System.Data.DbType.StringFixedLength
            param.IsNullable = true
            param.SourceColumn = "TownState"
            Me._adapter.InsertCommand.Parameters.Add(param)

This command works fine when run in a query window in VS 2005:
INSERT INTO Town
                      (Town_cd, TownName, TownState)
VALUES     (1234, 'Jamestown', 'NY')

The problem occurs when I click the Save button on the Binding Navigator on my form for a new row.
This is the code for that button:
Private Sub TownBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TownBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.TownBindingSource.EndEdit()
        Me.TownTableAdapter.Update(Me.UPC_MobileDataSet.Town)

End Sub

The exception occurs in the EndEdit method.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
bobgilpin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.