Solved

Visual studio  2012 datagrid view

Posted on 2016-08-09
11
48 Views
Last Modified: 2016-10-09
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
Comment
Question by:Nash Bawz
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 41748858
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41748864
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
 

Author Comment

by:Nash Bawz
ID: 41748897
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
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 41748917
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
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 41748922
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 8

Assisted Solution

by:Prakash Samariya
Prakash Samariya earned 200 total points
ID: 41748923
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
 

Author Comment

by:Nash Bawz
ID: 41751485
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
 
LVL 8

Accepted Solution

by:
Prakash Samariya earned 200 total points
ID: 41751559
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
 

Author Comment

by:Nash Bawz
ID: 41751657
Thanks a lot Prakash Sir for a valuable input. I will try and come back to u if any further clarification needed.
Rgds
1
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41763569
Thank you Nash for you comment, we are glad to help you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now