Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94
  • Last Modified:

Visual studio 2012 datagrid view

In Visual studio  2012 datagrid view when I delete a row the next row should automatically be assigned the deleted row Id. My data resides in My sql . kindly help me with the code.  Example if I have 5 rows 1,2,3,4 ,5 and delete no. 3 row the no. 4 row should become no. 3 and no.  5 should become no. 4 ( auto increment  is not  working).  kindly help me  with code and the procedure.
0
Nash Bawz
Asked:
Nash Bawz
  • 3
  • 3
  • 2
  • +1
5 Solutions
 
Fernando SotoRetiredCommented:
That is not what autoincrement does in SQL. From Microsoft documentation
A table can only contain one auto-increment field. The numbers assigned to this field will not always be consecutive for the following reasons:

If you delete some records from a table, the numbers used for these records are not reused.
If several transactions are performed at the same time, they will each be assigned a different number. However, if one of these transactions is rolled back, the number that it was assigned is not reused.

If you add an auto-increment field to an existing table, the field automatically generates consecutive values and inserts them into the table. If you enable the AutoIncrement property for a field that already contains data, there must be no zero values in the field.

The AutoIncrement property is designed to always assign numbers automatically. If you want to insert a record, be sure that the value in this field is blank before you insert the record. This is even more critical when you are using the SQL Server Option.
0
 
Éric MoreauSenior .Net ConsultantCommented:
it is not a usual request to change the ids when a row gets deleted. it is usually lost. there are many reasons varying from performance to data integrity. You normally let the holes in place.
0
 
Nash BawzAuthor Commented:
Thanks for quick reply. My requirement is to collect data and keep on adding new data   as and  when I receive, if some data is irrelevant to me I am required to delete it without any trace of  it in my records. That is the reason I want to renumber my row id as and when I delete any row  in between rows. Kindly let me know the procedure and code both in visual studio 2012 and my sql Work bench 6.3 ( I am using My sql as local host). I am just a beginner in this field and studying in class 12 ( kindly ignore my ignorance) so kindly be simple in your reply. Thankks
Rgds
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Éric MoreauSenior .Net ConsultantCommented:
Not sure about MySQL (BTW your question topics refer to MS SQL which is a different beast).

One thing you need to keep in mind is that you will need to lock down the data while you will be doing your changes (using a transaction) to prevent other people trying to update and delete data at the same time you are trying to update yours!

You also need to keep in mind that all the foreign keys will also need to updated. For example, if you are using that id in another table, you will have to update all of them (unless you used cascading update/delete).

I would use a cursor in MS SQL server to achieve all that. But since you are using MySQL, I can't tell you much more!
0
 
Fernando SotoRetiredCommented:
You state the following, "if some data is irrelevant to me I am required to delete it without any trace of  it in my records.". That is what will happens when you delete the record. The ID is used by the database for house keeping and is really not meant for user consumption. You really do not want to start reindexing the records.
0
 
Prakash SamariyaIT ProfessionalCommented:
What kind of problem you face if you do not renumber your column?

In general good practice, once you made entry in DB with some keys always remains with the row. If you delete row means it will delete key as well. Relation between tables, Sorting, Indexing and many other things performs based on this!

I advice to keep as it is!
0
 
Nash BawzAuthor Commented:
is there any way to reset the table Id number after I delete any in between row. I do not want to delete row Id,  I just want to delete row contents and the next subsequent row contents should move up so the row Id is always sorted out in sequence.  I. e. 1,2,3,4......
0
 
Prakash SamariyaIT ProfessionalCommented:
There is no direct way to do it! you can copy table data into TempTable, truncate table, reset identity and re-input data into table.

Please check code below: (please also ready the comment above each sql statement to understand the logic)
/*Store data into TEMP table*/
SELECT * 
INTO #TEMP
FROM [TESTSTAGE].[DBO].[PRODUCTDETAILS]

/*Set identity column ON (if not) in YOUR table*/
SET IDENTITY_INSERT [TESTSTAGE].[DBO].[PRODUCTDETAILS] ON

/*Truncate all data from YOUR table*/
TRUNCATE TABLE [TESTSTAGE].[DBO].[PRODUCTDETAILS]

/*Remove identity column from TEMP table*/
ALTER TABLE #TEMP DROP COLUMN PRODUCTID

/*Set identity column OFF in YOUR table*/
SET IDENTITY_INSERT [TESTSTAGE].[DBO].[PRODUCTDETAILS] OFF

/*Insert data from TEMP table into YOUR table*/
INSERT INTO [TESTSTAGE].[DBO].[PRODUCTDETAILS]
SELECT * FROM #TEMP

Open in new window

You can make one SP in DB and call that after your deletion of record!
1
 
Nash BawzAuthor Commented:
Thanks a lot Prakash Sir for a valuable input. I will try and come back to u if any further clarification needed.
Rgds
1
 
Prakash SamariyaIT ProfessionalCommented:
Thank you Nash for you comment, we are glad to help you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now