简体   繁体   中英

how to synchronize sql row number with the ID column automatically

I'm using a listbox with insert/update/delete buttons on ASP.NET side, when I delete a student, the student ID is not equal with ID column anymore. Therefore when I delete a student, the order in listbox is not equal to SQL side. How can I automatically synchronize these two? I have set primary key and Identity Specification Set to Active, for your information. and included screenshots of the problem below.

在此输入图像描述

An ASP.NET list box should have a key property as well as a value property.

Typically, the key property would be populated with the ID from the database... when deleting the selected item in the list box, you would delete based on the key property, not on the index of the selected list box item.

Here's a quick tutorial that shows how to populate a list box with key / value pairs .

Whatever you do, DON'T re-number the IDs in your database to be sequential. That will create huge problems for you.

Listbox items on ASP.NET must be controlled by ListBox1.SelectedValue , rather than SelectedIndex. SelectedIndex of an item can change once you delete an item and an order changes. It would be chaotic to do so.

However, ListBox1.SelectedValue would help you to control each item by its value (which is the EmployeeID, rather than order in SQL table. When you create a listitem, you must give the EmployeeID in this case to the HTML value of listbox, which will be handled with ListBox1.SelectedValue when you insert or update an item

the code when you retrieve data from SQL to the listbox should look like this:

cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName,EmployeeID FROM Employees", cnn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
    while (dr.Read())
    {
        ListBox1.Items.Add(new ListItem(dr.GetString(0) + " " + dr.GetString(1),dr.GetInt32(2).ToString()));
    }
}

cnn.Close();

What you are referring to as the 'ID column' is actually just the row number returned by the SSMS query. This has no relationship to the records returned, it is just the order that they were returned. Take for instance the following queries:

SELECT * FROM ali ORDER BY OgrenciID ASC

SELECT * FROM ali ORDER BY OgrenciID DESC

Both queries return the same data, just in a different order. SQL doesn't have a 'guaranteed' way it will sort the data, unless you supply order by. This means that after enough data is entered, your query won't always show 1, 2, 3. Instead it might show 1, 3, 2 unless you tell it otherwise.

The purpose of having an identity column is that it will ensure each row of data has a unique ID. Let's say you were to have a second table called Permissions that was linked to that table by the OgrenciID. You wouldn't want User 2 to get User 1's permissions if user 1 gets deleted.

If you want to delete all the data from that table and reset the ID column back to 1, you would either have to Truncate the table or reset the key after deleting the records. However, this will require a complete wipe of the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM