Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Declare @UID varchar(30)
Set @UID=@UserId
Set @UID= @UserId
SET @UserId = @UID+1;


INSERT INTO Tbl_Anti_Ragging2(UserId,CollegeId,Year,Duration,authoritites_Name,authoritites_Designation,authoritites_Contact)
VALUES(@UserId,@CollegeId,@Year,@Duration,@authoritites_Name,@authoritites_Designation,@authoritites_Contact)

i am sending multiple row at one time from my C# code and i want to insert when First save butoon then value save like 1
1
1
1
second time 2
2
2
2

What I have tried:

Above is my Query -

i am inserting value i want to know how to insert means query execute then

Userid make like 1
1
1
1
and when query second time execute then i will do userid +1 like-

2
2
2
2
so Please help me--
Posted
Updated 2-Apr-17 3:19am
Comments
[no name] 1-Apr-17 8:01am    
Use an identity column.
Member 12183079 1-Apr-17 8:15am    
No i am sending multiple textbox value on submit button and i want to make on submit button row create 1 and next time on submit button row create 2
[no name] 1-Apr-17 14:06pm    
Well if you want to do it wrong then go ahead.

1 solution

As @NotPoliticallyCorrect has already said, the correct way to do this is to use an Identity column. At the very least do not refer to this column as an ID - unless this is just a means of generating data and the UserId column on this table is actually a foreign key to another table.

You could just increment a number within your C# code - it would have to be a class-level ("global") variable. This is not a good design however - it assumes that this program will only ever be run as this instance by one user and once finished will not be needed again.

You could do this instead...
SQL
CREATE TABLE GenerateIDs
(
	NextId int identity(1,1),
	requestBy nvarchar(125)
)

If you create a Stored Procedure to update this table
SQL
CREATE PROCEDURE [dbo].GetId @who nvarchar(125)
AS
	INSERT INTO GenerateIDs VALUES(@who) 
	SELECT SCOPE_IDENTITY()

Then each time you want to increment that number run the Stored Procedure first to get the Id number to use.
 
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