1

I have a database of sql server 2008 which I am using to display in a gridview. I have written some code to add new rows in asp.net and C# as Code behind.

When ever a row is added through the Programming I have put some checking which will not allow the required values to be Null.

But, here comes my problem when ever any one of the user adds a new row manually by opening the Database, then a blank value is allowing in the Primary key column which is not 'Null value'.

So, here I have to restrict even not to allow blank values in primary key column, how can I solve this problem.

2 Answers2

2

You need a check constraint

ALTER TABLE [TableName]
    ADD CONSTRAINT [CK_PrimaryKeyNotEmpty]
        CHECK
        (
            LEN([ColumnName]) > 0
        )
Daniel Renshaw
  • 33,729
  • 8
  • 75
  • 94
0

I'm having trouble figuring out what you actual question is.

1) If you need to make a column not accept null values:

Add a constraint to the column in the db:

ALTER TABLE [Table] ALTER COLUMN [Column] INTEGER NOT NULL

See: Altering a column: null to not null


2) If you need to restrict your PK column to only certain values add a constraint:

ALTER TABLE Table ADD CONSTRAINT CK_Table_Column_Range CHECK ( Column >= 0 )

Community
  • 1
  • 1
garnertb
  • 9,454
  • 36
  • 38
  • Here, my problem is the Primary Key column is not accepting Null Values, but when ever we enter the cursor in the Column, and not giving any thing in that, but filling the remaining columns its accepting the row to be added even though nothing is given in the Primary key column. – Sai Kalyan Kumar Akshinthala Apr 25 '11 at 11:35