Search code examples
asp.netsql-serverwebforeign-keys

Transforming a contact into a client in a web form using SQL Server


I have a parent table called Customers, customers has the primary key CustomerId, Foreign Key ContactId, CustomerName, Adress, and JobTitle. The child table is called Contacts which has the primary key ContactId, ContactName, and Adress.

My Register Contact web GUI has textboxes with labels for the fields Name and Adress, seeing as I set the contact ID as identity it automatically increments.

When the fields are filled, I have a button that inserts the data into the DataBase. A second button provides the user with the option to transform the contact into a client transfering the previous info into the next form that is only missing the JobTitle info.

Transfering the the fields that the user types is easy enough using the following code.

Response.Redirect("FormularioContactos.aspx?Name=" + nom.Value);

and cathcing it in the client form's pageload code.

if (Request.QueryString["Name"] != null)
        nom.Value = Request.QueryString["Name"];

My question is how to send the ContactId that is automatically created when the contact info is inserted due to the identity property.


Solution

  • You can use SCOPE_IDENTITY() in sql insert statement to get the new ContactId value

    INSERT INTO Contacts (ContactName, Adress) VALUES ('Name', 'Address')
    SELECT SCOPE_IDENTITY()