protected int InsertIntoDatabase(string tableName, ArrayList values, List <string> keys) { mycon = Conection.MakeCon(); string fieldNames = string.Join(",", keys); string parameters = "@" + string.Join(",@", keys); string quary = "INSERT INTO " + tableName + "(" + fieldNames + ")" + " OUTPUT INSERTED.PatientID " + "VALUES" + "(" + parameters + ") "; SqlCommand cmd = new SqlCommand(quary, mycon); for (int i = 0; i < keys.Count; i++) { cmd.Parameters.AddWithValue("@" + keys[i], values[i]); } try { mycon.Open(); int ID = (int)cmd.ExecuteScalar(); mycon.Close(); return(ID); } catch (SqlException ex) { for (int i = 0; i < ex.Errors.Count; i++) { Console.WriteLine(ex.Errors[i].Message); Console.WriteLine(ex.Errors[i].LineNumber); Console.WriteLine(ex.Errors[i].Source); Console.WriteLine(ex.Errors[i].Number); } } return(0); }