Default value not working with bit data type

  • Hello,

    When viewing the Individuals table below I would expect to see 0 as the default value for the DuesPaid column but instead I see null. I also get the following error if I donโ€™t enter data in the DuesPaid Column:

    No row was updated

    The data in row 1 was not committed.

    Error Source: .Net SqlClient Data Provider.

    Error Message: String or binary data would be truncated

    If I attempt to enter a number in the DuesPaid Column I get this error:

    Invalid value for cell (row 1, column 6)

    The changed value in this cell was not recognized as valid.

    .Net Framework Data Type: Boolean

    Error Message:String was not recognized as a valid Boolean.

    Type a value appropriate for the data type or press ESC to cancel change.

    Table Columns are below:

    IndividualID INT Identity Primary Key, --leaving blank when entering test data

    FirstName Varchar NOT NULL, --test value Bill

    LastName Varchar NOT NULL, --test value Test

    Address Varchar NOT NULL, -- test address Janeway

    Phone Varchar --test value 333-333-3333

    DuesPaid bit NOT NULL DEFAULT 0 --test value 5 or null (errors for both attempts)

    Any assistance appreciated.

  • Can you post the .NET code that is generating the error?

    If you are trying to insert NULL it will fail because the column does not allow NULLs and if you are attempting to insert 5 then it will fail because the bit data type only allows 0 or 1.

  • I have attempted to add a row with 0,1, True and False in the DuesColumn but receive error prompts. Here is my code:

    CREATE Table Groups

    (GroupID INT Identity Primary Key,

    GroupName Varchar NOT NUll,

    Dues money Default 0 CHECK (Dues >= 0))

    CREATE Table Individuals

    (IndividualID INT Identity Primary Key,

    FirstName Varchar NOT NUll,

    LastName Varchar NOT NUll,

    Address Varchar NOT NUll,

    Phone Varchar) --Null allowed since the user may not have a phone

    CREATE Table GroupMembership

    (GroupID INT Identity REFERENCES dbo.Groups (GroupID),

    IndividualID INT NOT NUll REFERENCES dbo.Individuals (IndividualID))

    CREATE CLUSTERED INDEX IX_GroupID

    ON GroupMembership (GroupID)

    CREATE INDEX IX_IndividuaLID

    ON GroupMembership (IndividualID)

    ALTER TABLE Individuals

    ADD DuesPaid bit NOT NULL DEFAULT ((0))

  • Great I assume that the table is being built correctly in SQL Server, but your error is coming from a .NET application and I'd need to see the .NET code to try to identify what is causing the error and the SQL code being executed in .NET.

  • I have written all the scripts in SQL Server Mgt Studio. I'm not using .Net Studio.

    I am testing the table in SQL Server Mgt Studio.

  • Is there a way to see the generated .Net code from SQL Server Studio?

    I right click the table and see the script that SQL Server created for the INDIVIDUALS tabl this script look a little different from what I crated, here it is:

    USE [Membership]

    GO

    /****** Object: Table [dbo].[Individuals] Script Date: 09/16/2008 09:32:18 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[Individuals](

    [IndividualID] [int] IDENTITY(1,1) NOT NULL,

    [FirstName] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

    [LastName] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

    [Address] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

    [Phone] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

    [DuesPaid] [bit] NOT NULL DEFAULT ((0)),

    PRIMARY KEY CLUSTERED

    (

    [IndividualID] ASC

    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

  • What happens when you run this in SSMS:

    Insert Into dbo.Individuals

    (

    FirstName,

    LastName,

    Address

    )

    Values

    (

    'John',

    'Doe',

    '1 First Street'

    )

  • When I executed this query I received this error message:

    Msg 8152, Level 16, State 14, Line 1

    String or binary data would be truncated.

    The statement has been terminated.

  • I actually expected that error because your varchar columns are all defined with a length of 1. This is the number in parentheses. When you define a character column (char/varchar/nchar/nvarchar) you need to define a length. You could change my insert to just insert 1 character in each of the columns to test the bit column, but you really should re-create the table with appropriate lengths for your columns and then re-run the insert.

  • Its working now. ๐Ÿ˜€

    Thanks for the help.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply