How to Get Id from Database where date saved ?

When i call PostASync some entered data saved under a unique Id .. All i want to get that Id at the same time..
Please Help

Answers

  • NMackayNMackay GBInsider, University mod

    Assuming you mean your posting some values to a restful API and inserting the data then just return a response object with the database ID.

    We return a DTO that has the rows affected and a server response object.

  • MabroukMabrouk USMember ✭✭✭

    Hi,

    What you need is to not focus your design in the 'AutoIncrement' ID of the table.. But try to use an other id like Guid.
    For example: Lets say you have Person class

    public class Person
        {
            public string Id { get; set; }
            public string FullName { get; set; }
        }
    

    When you save data, you can so something like :

    public void Save(Person person)
            {
                if(string.IsNullOrEmpty(person.Id))
                {
                    person.Id = Guid.NewGuid().ToString(); // You have the id before sending it to server.
                    // INSERT NEW PERSON INTO TABLE
                    return;
                }
                // UPDATE AN EXISTING PERSON
            }
    

    I hope that helps,
    Mabrouk.

  • NMackayNMackay GBInsider, University mod

    @Mabrouk said:
    Hi,

    What you need is to not focus your design in the 'AutoIncrement' ID of the table.. But try to use an other id like Guid.
    For example: Lets say you have Person class

    public class Person
        {
            public string Id { get; set; }
            public string FullName { get; set; }
        }
    

    When you save data, you can so something like :

    public void Save(Person person)
            {
                if(string.IsNullOrEmpty(person.Id))
                {
                    person.Id = Guid.NewGuid().ToString(); // You have the id before sending it to server.
                    // INSERT NEW PERSON INTO TABLE
                    return;
                }
                // UPDATE AN EXISTING PERSON
            }
    

    I hope that helps,
    Mabrouk.

    I've never seen that approach before, from experience with SQL replication guid's can clash occasionally, we used a compound key and a guid, generally I'd always recommend the server returns the ID as is charge of that.

  • FarhanFarriFarhanFarri USMember ✭✭

    As i'm using web api so how i get id on returns Ok(db.TableName);

Sign In or Register to comment.