I refer to the checked answer "How to get last inserted id?"
So far here is what I've done.
SqlCommand cmdInsert = new SqlCommand("INSERT INTO normal_asset(n_asset_serialNo,n_asset_propertyNo,n_asset_brandModel,n_asset_unitCost,n_asset_acquisitionDate,equitype_id,region_id) OUTPUT INSERTED.N_Asset_ID VALUES ('" + assetSerial + "','" + assetProperty + "','" + assetBrand + "','" + assetCost + "','" + assetAcquisition + "','" + assetEquipment + "','" + Session["usersRegion"].ToString() + "')");
cmdInsert.CommandType = System.Data.CommandType.Text;
cmdInsert.Connection = conn;
cmdInsert.ExecuteNonQuery();
Decimal newId = (Decimal)cmdInsert.ExecuteScalar();
SqlCommand cmdInsert2 = new SqlCommand("INSERT INTO asset(n_asset_id,office_id,asset_status,asset_accountable,asset_remarks) VALUES ('" + newId + "','" + inputOffice + "','" + inputStatus + "','" + inputAccountable + "','" + inputRemarks + "'");
cmdInsert2.CommandType = System.Data.CommandType.Text;
cmdInsert2.Connection = conn;
cmdInsert2.ExecuteNonQuery();
Then I want to get the appropriate id which is n_asset_id
.
The problem is the cmdInsert
is doubling its insert (maybe in my if condition) but I can't make the Decimal newId = (Decimal)cmdInsert2.ExecuteScalar();
to run correctly. I tried making it an Int. then string -> convert to int but still no luck.