Deleting identity row of datagridview control in vb.net - vb.net

I have done a project for a jewelry shop in vb.net . Right now almost it has been done. But the problem is, If i want to delete a row in datagridview control , the next identity rows' s.no should get decreased as well as in sql table for it.Please help me with the code.
Thanks!!!

as per my knowledge generally we cannot update the identity column
you can go through
Update values in identity column
other alternate could be i dont know whether it will work or not, but you can give a try
on delete trigger of table
you can set identity seed off, then fire query to update with its -1 value
and then find the max value of column and reset your identity by doing identity again on and with this command
DBCC CHECKIDENT (yourtable, reseed, 34)

Related

SQL Server : alter the Identity seed

I am migrating data from one database to another. I have my scripts mostly together already, but I am trying to figure out the best way to make one change to a table in the new database.
I have a Customer table. That table has a customer_id column which is the identity column. I want to change the identity seed/increment from (1,1) to (200,1) without changing the customer_ids for the existing data I will be inserting into the table.
Old data is 101-108. Basically we want to keep the old data the same so it matches up with old records in other systems, but we want the new data to start seeding in at 200.
I tried Googling how to do this, but all my Googling came back with results where people wanted to change what column was the identity column, and not just change the identity seed number. Is there a simple query I can use to accomplish what I want to do?
You can use DBCC CHECKIDENT:
DBCC CHECKIDENT ('dbo.customer', RESEED, 200)
This will change the current seed value of the identity column of the specified table. If you need to insert specific identity values, you can SET IDENTITY_INSERT ON in your insert statement.
IDENTITY_INSERT
What I would do unset the new column as an identity (using alter table), then insert the data from the old table, and then reset the new column as the identity again, with whatever increment you want as per the link
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017

SQL set identity and retain values

I have a problem restoring a table.
I wanted to restore data by copying data from the backup to the table.
The problem is, the PK of the table has the Identity-Property set. So when I inserted the lost rows they got new IDs.
I created a new table without identity and put the data in there.
Now I want to turn on Identity on the PK column, which doesn't work.
Any what I can do or if this is possible at all?
You cannot add IDENTITY property to existing column. Either you insert in existing table with IDENTITY column with IDENTITY_INSERT ON' option or you create a newIDENTITY` column in the new table.
This post might help you
This will solve your problem
[https://stackoverflow.com/a/1049305/6652909]
If you have backup of table then TRUNCATE your table then set primary key then make it auto increment in table and then copy your table and make sure your data of table is entered properly, it worked for me may be it will helpful to you also.

How do I reorder or auto-increment the primary key of a table if it is not being incremented properly?

I inherited a SQL Server database and for some reason the primary key is not being incremented properly.
For example, the PK for the last record in the table is 120350
Then, when I enter a new record into the table, it gets the PK 48377
What would be the best way to reorder or set the initial PK for new records? This is for Microsoft SQL Server 2008 R2.
DBCC CHECKIDENT('<tablename>');
This will reseed the identity column to where it should be (in this case 120351). If you want to reorder the existing value then you'll need to use SET IDENTITY_INSERT '<tablename>' ON as part of a script to move the rows around. How you do that depends on what exactly you're trying to accomplish. Keep in mind that you'll also need to keep any foreign keys in sync.
If you have some other requirements on the column that defines, "incremented properly" then you might need something besides an IDENTITY column.
When I use
DBCC CHECKIDENT('UPS_Worldship_Export');
I got this result: Checking identity information: current identity value '48377', current column value '120351'.
Somehow, the value was offtrack. Thanks to the comments above, I just reseeded the value to the highest value and it works correctly now.
DBCC CHECKIDENT (',<table name>', RESEED, <current column value>);

How do you UPDATE an identity column in SQL azure? Is it possible?

I have this code:
ALTER TABLE FishSticks
ADD MyNewIdentityColumnId INT IDENTITY(1,1) NOT NULL
SET IDENTITY_INSERT FishSticks ON
UPDATE FishSticks
SET MyNewIdentityColumnId = MyOldColumnId
However it says:
Cannot update identity column 'MyNewIdentityColumnId'.
I assume this is due to SQL Azure, but I didn't have much success googling. It seems that changing a column to be an identity while preserving data is a huge difficulty.
SET IDENTITY_INSERT only works for INSERTS and that error is not specific to SQL Azure. You'll get the same error on SQL Server/Express
to go around the issue, SET IDENTITY_INSERT to ON, reinsert all the column values from the existing row whose identity value you want to replace with a new value, SET IDENTITY_INSERT OFF, then delete the previous row.
I found this Possible solution from Herve Roggero who basically gives a way of turning off the ID and then adding the rows, then turning ID back on.

How to force SQL Server 2008 to not change AUTOINC_NEXT value when IDENTITY_INSERT is ON?

I got question about IDENTITY_INSERT. When you change it to ON, SQL Server automatically changes AUTOINC_NEXT value to the last inserted value as identity.
So if you got only one row with ID = 1 and insert row with ID = 100 while IDENTITY_INSERT is ON then next inserting row will have ID = 101. I'd like it to be 2 without need to reseed.
Such behaviour already exists in SQL Server Compact 3.5. Is it possible to force SQL Server 2008 to not change AUTOINC_NEXT value while doing insert with IDENTITY_INSERT = ON ?
I'm not aware of any way to prevent this behavior - after all, it's a prudent thing to do, anyway! If you've already set a value x (whatever that value might be), you shouldn't leave your seed value lower than x since otherwise, you're bound to run into getting a value in your IDENTITY column that's already there - not a good place to be!
But if you must, you can re-seed your IDENTITY column after you're done with your inserts using:
DBCC CHECKIDENT ('YourTableName', RESEED, 300);
where you can set any arbitrary value as your new seed value (here: 300). Of course, you need to be extra careful not to create any duplicates by setting the reseed value too low.
Additionally, if you do a reseed and your identity column is also a primary key, when you get back to the originally inserted value when IDENTITY_INSERT tablename ON was set you will get a PK violation. Another thing to think about.
Why would you want to do that? In the first place, there should be almost no occasions where you set identity insert to ON in a production system. And what difference would it make if you skip some? You can't count on identities not skipping in any event since it will skip if you do a rollback.