Pekaro
Pekaro

Reputation: 1

SQL INSERT an ID of freshly inserted row

im writing a library database program. It can insert books, but I have a problem in making a reference between book and a person which rents it. I can't get a last inserted id from a rents table to put it to the compilation table to assign book to a person who rents it. I've tried SCOPE_IDENTITY() but it doesn't works for me. Here's the code:

private void addRentButton_Click(object sender, EventArgs e) {
        elibrary f1 = new elibrary();

        string query = "INSERT INTO rents VALUES (@renterName, @rentStartDate, @rentEndDate)";
        using(f1.Connection = new SqlConnection(f1.connectionString))
        using(SqlCommand command = new SqlCommand(query, f1.Connection)) {
            f1.Connection.Open();
            command.Parameters.AddWithValue("@renterName", rentNameBox.Text);
            command.Parameters.AddWithValue("@rentStartDate", DateTime.Now);
            command.Parameters.AddWithValue("@rentEndDate", rentEndDatePicker.Value);

                command.ExecuteScalar();

        }
        rentEndDatePicker.Value = DateTime.Now;
        string Compilationquery =" INSERT INTO compilation VALUES (@bookId, SELECT SCOPE_IDENTITY())";

        using(f1.Connection = new SqlConnection(f1.connectionString))
        using(SqlCommand command = new SqlCommand(Compilationquery, f1.Connection)) {
            f1.Connection.Open();                
                command.Parameters.AddWithValue("@bookId", f1.listBook.SelectedValue);

            command.ExecuteScalar();

Upvotes: 0

Views: 98

Answers (2)

paparazzo
paparazzo

Reputation: 45096

You have disposed the command so SCOPE_IDENTITY() is gone. There is no reason to dispose of the commmand twice.

    using(SqlCommand command = new SqlCommand(query, f1.Connection)) 
    {
        f1.Connection.Open();
        command.Parameters.AddWithValue("@renterName", rentNameBox.Text);
        command.Parameters.AddWithValue("@rentStartDate", DateTime.Now);
        command.Parameters.AddWithValue("@rentEndDate", rentEndDatePicker.Value);
        command.ExecuteScalar();
        int id = (Int32)command.ExecuteScalar();

        command.Parameters.Clear();
        Compilationquery = "INSERT INTO compilation VALUES (@bookId, @id)";  
        command.CommandText = Compilationquery;      
        command.Parameters.AddWithValue("@bookId", f1.listBook.SelectedValue);
        command.Parameters.AddWithValue("@id", id);
        command.ExecuteScalar();
    } 

Upvotes: 0

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

Actually, you are not retrieving the last inserted ID value from the first query, since the SCOPE_IDENTITY() is wrongly placed and you are not assigning the ExecuteScalar() return value anywhere:

String query = "INSERT INTO rents VALUES (@renterName, @rentStartDate, @rentEndDate); SELECT CONVERT(INT, SCOPE_IDENTITY())"; // "SELECT CAST(SCOPE_IDENTITY() AS INT)" can also be an option
Int32 lastId = 0;

using (f1.Connection = new SqlConnection(f1.connectionString))
using (SqlCommand command = new SqlCommand(query, f1.Connection))
{
    f1.Connection.Open();

    command.Parameters.AddWithValue("@renterName", rentNameBox.Text);
    command.Parameters.AddWithValue("@rentStartDate", DateTime.Now);
    command.Parameters.AddWithValue("@rentEndDate", rentEndDatePicker.Value);

    lastId = (Int32)command.ExecuteScalar();
}

Once this is done, you can proceed with the second query as follows:

String compilationQuery = "INSERT INTO compilation VALUES (@bookId, @rentId)";

using (f1.Connection = new SqlConnection(f1.connectionString))
using (SqlCommand command = new SqlCommand(compilationQuery, f1.Connection)) 
{
    f1.Connection.Open();

    command.Parameters.AddWithValue("@bookId", f1.listBook.SelectedValue);
    command.Parameters.AddWithValue("@rentId", lastId);
    // ...

Upvotes: 1

Related Questions