views:

210

answers:

4

Hello all, I have problem with gridview deleting.I have table name Doctor with Id,Name,Address,Phone.Id is auto generated field.After adding data when i am displaying in gridview then if delete any id from gridview Again then if i add any new details from the form its starting from the new number.I mean if i delete the last id no 5 then again if i add any new doctor its taking id value 6 not from 5.My query is it should start again from 5.Here is my code.Pls help me.

public class Doctor
{  
  public int Id { get; set; }
  public string Name { get; set; }
  public string Address { get; set; }
  public string Phone { get; set; }
}

public static class DoctorDataLayer
{
  public static void AddDoctor(Doctor doctor) 
  {
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // JohannesH: Changed from .ToString() to .ConnectionString
    using(var connection = new SqlConnection(connectionString))
    {
      using (var command = new  SqlCommand("insert into doctor values(@name,@address,@phone)", connection))
      {      
        command.Parameters.AddWithValue("@name", doctor.Name);
        command.Parameters.AddWithValue("@address", doctor.Address);
        command.Parameters.AddWithValue("@phone", doctor.Phone);
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
      }
    }
  }
}
public static class DoctorBusinessLayer
{
  public static void CreateDoctor(string name, string address, string phone) 
  {
    DoctorDataLayer.AddDoctor(new Doctor {Name = name, Address = address, Phone = phone});
  }
}
+1  A: 

So the Id is created by the database (autonumber). When id 5 is used it's used up. This is normal behavior.

Torbjørn
+3  A: 

This is perfectly normal database behaviour and has nothing to do with your GridView. If you have an issue with gaps in autogenerated (identity) columns, either use your own logic to generate unique ID's or use custom SQL scripts to check for gaps in Identity values and fill those gaps.

Example B in the Transact-SQL reference shows a way to do just this.

Cerebrus
+1  A: 

As other have noted, if this is an autogenerated ID from the DB then once it is used it will not be regenerated, each ID is unique regardless if the data still exists or not. If IDs were recycled you could get into issues with foreign references that may have pointed to the old item with that ID and now would point to a new different record with the reused ID.

Typically you don't expose the IDs to the user anyway so it is a non issue.

schooner
+1  A: 

You shouldn't depend on autogenerated ids sequences being ordered or not having gaps. As others have noted, the behavior you are seeing is perfectly normal behavior for an autogenerated id and to make it otherwise you'll need to jump through a lot of hoops. If you need the ids to be ordered by the insertion sequence, you should put in an autogenerated date/time field and then select the data ordered by that field (and index it). That way if you ever decide to switch from a numeric id to a GUID or some other id format in which the sort order is different than the insertion order your data will still be ordered correctly. If you need to have a "place order" for each, generate that automatically (say a rownumber) as you are selecting ordered by date. That way you will still have strict numerical ordering even if records get deleted later.

tvanfosson