I've got the following code:

```
private void btnAddMatter_Click(object sender, EventArgs e)
{
    MatterCode = "";
    EntityID = 0;
    FeeEarnerID = 0;
    OpenedByUserID = 0;
    CompanyContactDetailsID = 0;
    Description = "";
    OurReference = "";
    TheirReference = "";
    DateOpened = DateTime.Now;
    MatterTypeID = 0;
    DepartmentID = 0;
    ResponsibleUserID = 0;
    TrustBankAccountID = 0;
    BusinessBankAccountID = 0;

    string connectionString = "Data Source=***\\SQLEXPRESS;Initial Catalog=STUPELG;Persist Security Info=True;User ID=***;Password=***";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO dbo.Matter ( MatterCode, EntityID, FeeEarnerID, OpenedByUserID, CompanyContactDetailsID, Description," +
                                        " OurReference, TheirReference, DateOpened, MatterTypeID, DepartmentID, ResponsibleUserID, TrustBankAccountID, BusinessBankAccountID)" +
                                        " VALUES ( @MatterCode, @EntityID, @FeeEarnerID, @OpenedByUserID, @CompanyContactDetailsID, @Description," +
                                        " @OurReference, @TheirReference, @DateOpened, @MatterTypeID, @DepartmentID, @ResponsibleUserID, @TrustBankAccountID, @BusinessBankAccountID);" +
                                        " SELECT SCOPE_IDENTITY();");
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        cmd.Parameters.AddWithValue("@MatterID", MatterID);
        cmd.Parameters.AddWithValue("@MatterCode", MatterCode);
        cmd.Parameters.AddWithValue("@EntityID", EntityID);
        cmd.Parameters.AddWithValue("@FeeEarnerID", FeeEarnerID);
        cmd.Parameters.AddWithValue("@OpenedByUserID", OpenedByUserID);
        cmd.Parameters.AddWithValue("@CompanyContactDetailsID", CompanyContactDetailsID);
        cmd.Parameters.AddWithValue("@Description", Description);
        cmd.Parameters.AddWithValue("@OurReference", OurReference);
        cmd.Parameters.AddWithValue("@TheirReference", TheirReference);
        cmd.Parameters.AddWithValue("@MatterTypeID", MatterTypeID);
        cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
        cmd.Parameters.AddWithValue("@ResponsibleUserID", ResponsibleUserID);
        cmd.Parameters.AddWithValue("@TrustBankAccountID", TrustBankAccountID);
        cmd.Parameters.AddWithValue("@BusinessBankAccountID", BusinessBankAccountID);
        cmd.Parameters.AddWithValue("@DateOpened", DateOpened);
        connection.Open();
        cmd.ExecuteNonQuery();

        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet NewMatterID = new DataSet();
        adapter.Fill(NewMatterID);
        MatterCode = Convert.ToString(NewMatterID.Tables[0].Rows[0][0]);
    }

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("UPDATE Matter SET MatterCode = @MatterCode WHERE MatterID = " + MatterCode);

        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;

        cmd.Parameters.AddWithValue("@MatterCode", MatterCode);

        connection.Open();
        cmd.ExecuteNonQuery();
    }

    MessageBox.Show("Matter " + MatterCode + " successfully created");
}

```

After the row is inserted, the new MatterID (primary key that is generated automatically) should be copied to the MatterCode field. Currently it works EXCEPT that there is an extra row that is generated at when the button is clicked:

[![enter image description here](https://i.stack.imgur.com/Eyl76.png)](https://i.stack.imgur.com/Eyl76.png)

How do I fix this???