Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Default value not working with bit data type Expand / Collapse
Author
Message
Posted Monday, September 15, 2008 6:47 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
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.
Post #569889
Posted Tuesday, September 16, 2008 6:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
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.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #570156
Posted Tuesday, September 16, 2008 7:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
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))
Post #570239
Posted Tuesday, September 16, 2008 8:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
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.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #570249
Posted Tuesday, September 16, 2008 8:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
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.
Post #570277
Posted Tuesday, September 16, 2008 8:33 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
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
Post #570286
Posted Tuesday, September 16, 2008 9:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
What happens when you run this in SSMS:

Insert Into dbo.Individuals
(
FirstName,
LastName,
Address
)
Values
(
'John',
'Doe',
'1 First Street'
)





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #570363
Posted Tuesday, September 16, 2008 9:55 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
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.
Post #570397
Posted Tuesday, September 16, 2008 10:10 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
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.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #570415
Posted Tuesday, September 16, 2008 11:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 25, 2011 7:44 AM
Points: 25, Visits: 51
Its working now. :D
Thanks for the help.
Post #570496
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse