Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Default value not working with bit data type


Default value not working with bit data type

Author
Message
t-pinto
t-pinto
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
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.
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11020 Visits: 14858
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
t-pinto
t-pinto
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
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))
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11020 Visits: 14858
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
t-pinto
t-pinto
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
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.
t-pinto
t-pinto
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
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
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11020 Visits: 14858
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
t-pinto
t-pinto
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
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.
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11020 Visits: 14858
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
t-pinto
t-pinto
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 51
Its working now. BigGrin
Thanks for the help.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search