Coding Villa Forum
FAQs
Advertisements

Value of Database column ID is not incrementing.

Iram Online 5/31/2012 11:49:39 AM

Hello Friends!

I have designed a table with ID value as primary key and set its identity specification to auto increment. However, value of ID column is not incrementing. I am not understanding the exact cause behind that problem. Is it a coding error or a database error?

Please friends waiting for your guidance.

Thanks

Share with Friends

Advertisements

Answer

Mehmood Online 5/31/2012 2:11:21 PM

Could you post the complete table script as I mentioned in my other reply of your post.

Follow the steps and post your script.

Also post your inserts please.

Answer

Mehmood Online 5/31/2012 5:36:29 PM

Execute the below script and repond back to me whether it
Works or not for you.

USE [master]

GO



Create table abc (
[Id] [int] IDENTITY (1,1) NOT NULL,
[Name] [varchar] (5) NULL,

CONSTRAINTS [pk_id] PRIMARY KEY NONCLUSTERED
(
[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



insert into abc (Name)
Values ('waqas')

Answer

Mehmood Online 5/31/2012 5:38:14 PM

SEE THE value in table abc now. Table will be created in master database

Answer

Mehmood Online 5/31/2012 7:10:41 PM

SEE THE value in table abc now. Table will be created in master database

Answer

Iram Online 6/5/2012 12:21:58 PM

Thanks Mehmood for your replies. My problem is solved. Actually I Declared ID in stored procedure as an output and does not specify that in DAL. Following is my stored procedure.


ALTER PROCEDURE [dbo].[Proc_CouponDetails_AddNew]
(
@CouponID INT OUTPUT,
@CatID INT,
@Title NVARCHAR(50),
@Start_Date datetime,
@AddDate datetime,
@Exp_Date datetime,
@Inactive BIT,
@Code TEXT,
@Link NVARCHAR(200),
@Image NVARCHAR(200),
@PageTitle NVARCHAR(100),
@MetaKeywords NVARCHAR(500),
@MetaDescription NVARCHAR(500),
@Slug NVARCHAR(50)
)
AS
BEGIN
SET NOCOUNT ON;

SET @CouponID = ISNULL((SELECT MAX(CouponID) + 1 FROM CouponDetails WITH (NOLOCK)),1)
IF @CouponID IS NULL
SET @CouponID = 1
INSERT INTO [CouponDetails]
(CatID,Title,Start_Date, AddDate, Exp_Date,Inactive,
Code,Link ,Image,PageTitle, MetaKeywords, MetaDescription, Slug)
VALUES
(@CatID, @Title, @Start_Date, @AddDate, @Exp_Date,@Inactive,
@Code,@Link,@Image, @PageTitle, @MetaKeywords, @MetaDescription, @Slug)

END

Adding following line of code in my DAL class solved my problem. Now Value of ID is incrementing.

objCmd.Parameters.Add("@CouponID", SqlDbType.Int).Direction = ParameterDirection.Output;

Share with Friends

Post reply

 

Enter This Code
Captcha
 

Subscribe

Email me when people reply


Subscribe to the