Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I'm creating the sql database table I not allow nulls.But when I'm entering data through windows application .If i not give value it use 0 as data.
What should i do.should i have to write coding for that.please help me.I could not find a way attach table/table definition pictures.
Posted
Comments
Nissim Salomon 1-Sep-13 0:30am    
Can you please be more specific about the problem ? are you using EF, ADO, NH ? please provide an example
pradiprenushe 1-Sep-13 0:31am    
give some default value for that column

1 solution

If you do not allow nulls in a column, then SQL has to do one of two things when you insert a new record, but don't supply a value:
1) Complain and reject the insert with an exception.
2) Use a default value for the column, if set.

Numeric values have a default of zero (unless you change that in your table definition), but others cannot be assigned a default value because there is no sensible default that can be applied.

In your windows application, if you do not have a value, then I would suggest that you supply one which makes sense to your windows application as a "no value assigned": for a string column, that would be an empty string, for numbers I generally use -1 or the maximum value, or I allow nulls and check for them!

Check your table definition, and see what default you have allowed.

BTW: You can post table definitions here very easily: just go to SSMS, right click the table, and select "Script Table as...", "CREATE To...", "Clipboard". You can then paste that directly into your question:
SQL
USE [Testing]
GO

/****** Object:  Table [dbo].[MyTable]    Script Date: 09/01/2013 09:08:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[MyTable](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](50) NULL,
	[Model] [varchar](50) NULL,
 CONSTRAINT [PK_MyTable_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900